Friday, July 4, 2008

Edit,Update,delete in gridview using Xml file





TemplateField HeaderText="empId" >




TextBox ID="txtid" runat =server Text='<%#Eval("empid") %>'>
EditItemTemplate>
TemplateField>
TemplateField HeaderText="empname" >




TextBox ID="txtname" runat =server Text='<%#Eval("empname") %>'>
EditItemTemplate>
TemplateField>
empcity" >




TextBox ID="txtcity" runat =server Text='<%#Eval("empcity") %>'>


empsalary" >




TextBox ID="txtsalary" runat =server Text='<%#Eval("empsalary") %>'>


CommandField ShowEditButton="True" />
CommandField ShowDeleteButton="True" />















codefile

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
}
}
void binddata()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("empdata.xml"));
if (ds != null && ds.HasChanges())
{
gv.DataSource = ds;
gv.DataBind();
}
else
{
gv.DataBind();
}
}
protected void Editdata(object s, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
binddata();
}
protected void Deletedata(object s, GridViewDeleteEventArgs e)
{
binddata();
DataSet ds= gv.DataSource as DataSet;
ds.Tables[0].Rows[gv.Rows[e.RowIndex].DataItemIndex].Delete();
ds.WriteXml(Server.MapPath("empdata.xml"));
binddata ();
}
protected void Canceldata(object s, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
binddata();
}
protected void Updatedata(object s, GridViewUpdateEventArgs e)
{

int i =gv.Rows[e.RowIndex].DataItemIndex;
string id=(gv.Rows[e.RowIndex].FindControl("txtid") as TextBox).Text;
string name=(gv.Rows[e.RowIndex].FindControl("txtname") as TextBox).Text;
string city=(gv.Rows[e.RowIndex].FindControl("txtcity") as TextBox).Text;
string salary=(gv.Rows[e.RowIndex].FindControl("txtsalary") as TextBox).Text;
gv.EditIndex = -1;
binddata();
DataSet ds =(DataSet) gv.DataSource;
ds.Tables[0].Rows[i]["empid"] = id;
ds.Tables[0].Rows[i]["empname"] = name;
ds.Tables[0].Rows[i]["empcity"] = city;
ds.Tables[0].Rows[i]["empsalary"] = salary;
ds.WriteXml(Server.MapPath("empdata.xml"));
binddata();
}
protected void pageddata(object s, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
binddata();
}

protected void insert(object sender, EventArgs e)
{
binddata();
DataSet ds = gv.DataSource as DataSet;
DataRow dr = ds.Tables[0].NewRow();
dr[0] = empId.Text;
dr[1] = empName.Text;
dr[2] = empcity.Text;
dr[3] = empsalary.Text;
ds.Tables[0].Rows.Add(dr);
ds.AcceptChanges();
ds.WriteXml(Server.MapPath("empdata.xml"));
binddata();
empId.Text = string.Empty;
empcity.Text = string.Empty;
empName.Text = string.Empty;
empsalary.Text = string.Empty;
}
xml file









29 comments :

  1. it is very helpful.I am very much thankful to u.

    ReplyDelete
  2. Iam very much thankful to you

    ReplyDelete
  3. U didnt say anything abt tht XML file....
    Initially how to create that XML file....Plz say abt that.

    ReplyDelete
  4. where the hell is .xml file

    ReplyDelete
  5. Yes I'd like to know the structure of your XML file as well.

    ReplyDelete
  6. Please tell me the .xml file structure.

    Thanks...

    ReplyDelete
  7. thank u so much its very very helpful to me and everyone

    ReplyDelete
  8. Thank You very much

    ReplyDelete
  9. hi could you check the update method its not updating the content..

    ReplyDelete
  10. There is an error in the update method. When you get the rowindex from the page, since the number of rows to be displayed in the page is always 3 , the index gets limited to 1,2 or 3...therefore the update method always updates the record 1,2 or 3 in the xml file. This would require a bit more processing than just updating with the index.

    ReplyDelete
  11. 程序运行良好,谢谢你的文章

    ReplyDelete
  12. Hey Thanks a "Ton" :) for posting this code. I cannot express how happy I am after finding this post of yours. I have my project submission on Tuesday. The only thing pending in my project was inserting, updating and deleting data from xml file. I tried very hard to find the solution but I couldn't succeed. I started searching for the answer on internet and I found your post. :)
    I just can say that I am very much thankful to you. I want to say this with all my heart. May almighty Allah always bless you with peace, happiness, good health and prosperity. Ameen

    ReplyDelete
  13. It is very helpful to learners.
    Thank u very much.
    #VINOD#
    New-Delhi

    ReplyDelete
  14. Its very nice code.
    Thanks... :)

    ReplyDelete
  15. Thanks it is very good code.
    I have made little change as the last XML element is deleted it throws an exception.
    Following change will fix that issue.

    private void binddata()
    {
    DataSet ds = new DataSet();
    ds.ReadXml(xmlPath);

    if (ds != null && ds.HasChanges())
    {
    gv.DataSource = ds;
    gv.DataBind();
    }
    else
    {
    gv.DataBind();
    }
    }

    ReplyDelete
  16. The Updatedata fails when you edit items from page 2 and more. Please replace the int int i = e.RowIndex;

    by this line of code.

    int i = gridViewProjects.Rows[e.RowIndex].DataItemIndex;

    ReplyDelete
  17. very good but some erorr inthe code.............

    ReplyDelete
  18. its very nice to understand the coding...

    ReplyDelete
  19. Nice Code but I've got into a trouble that I am unable to fix even after googling for hours. As I load the page the GridView is showing the number of rows equal to number of entries in the XML file but the text in the nodes are not displaying in table rows at all. The "Edit" and "Delete" button is there in each row. Moreover when I click on the "Edit" button of a row, the text boxes only populate the "Empcity" and "Empsalary" fields with correct data, but other fields even doesn't displays the Editing Text Boxes. Kindly help me in fixing the issue. its urgent for me. Thanks

    ReplyDelete
  20. Excellent Man..... Works Perfect 4 me... once again Thanx.....

    ReplyDelete
  21. Every now and then someone posts elegant code which makes life a whole lot easier. This is one of those times. Thanks. The answer to Naveen is to place a asp label with text eval to property to see the item on loading

    ReplyDelete
  22. Xml Structure gets change to keep same just values should update..what should i do for same that

    ReplyDelete

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath