dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersBibillis
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » ASP.NET GridView

Save,Update and Delete in Xml file in Asp.Net


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 7


Save,Update and Delete in Xml file in Asp.Net



 


XML is designed to store and transport data. We can store data in a Xml file or Can use Xml as a databse.
Here is and example of how we can use xml file to store data,
update data and also delete data in Xml file using Asp.Net and C#.

I am using a Grid View control for saving,updating and deleting data in Xml File

use the following code in your .aspx file. it will create a grid view as needed to operate on xml file



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" ShowFooter="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowCommand="GridView1_RowCommand"
OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="Eno">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("ename") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("ename") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("eadd") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("eadd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("esal") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("esal") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="save">Save</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<EmptyDataTemplate>
No Data Available
</EmptyDataTemplate>
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>




I am using following namespaces in my .cs file (Code File)



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;



Now Copy paste the Following Code in Your Page_Load Event in .cs File



String st = Server.MapPath("emp.xml");
if (Page.IsPostBack == false)
{
if(File.Exists(st)==false)
createxml();
getxml();
}



Now add the following code below the Page_Load Event



//Method Creating Xml File if does'nt Exists
private void createxml()
{
DataTable tb = new DataTable("emptable");
tb.Columns.Add("eno", Type.GetType("System.Int32"));
tb.Columns.Add("ename", Type.GetType("System.String"));
tb.Columns.Add("eadd", Type.GetType("System.String"));
tb.Columns.Add("esal", Type.GetType("System.Int32"));
DataRow r = tb.NewRow();
r[0] = 1;
r[1]="Rahul Choudhary";
r[2] ="Chandigarh";
r[3] =12000;
tb.Rows.Add(r);
String st = Server.MapPath("emp.xml");
tb.WriteXml(st);
}

//Method to bind Grid view
private void getxml()
{
String st = Server.MapPath("emp.xml");
DataSet ds = new DataSet();
ds.ReadXml(st);
GridView1.DataSource = ds;
GridView1.DataBind();
}

//Grid View Event to Save Data into Xml File
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "save")
{
String st = Server.MapPath("emp.xml");
DataSet ds = new DataSet();
ds.ReadXml(st);
DataRow r = ds.Tables[0].NewRow();
r[0] = Convert.ToInt32(((TextBox)(GridView1.FooterRow.FindControl("TextBox1"))).Text);
r[1] = ((TextBox)(GridView1.FooterRow.FindControl("TextBox3"))).Text;
r[2] = ((TextBox)(GridView1.FooterRow.FindControl("TextBox5"))).Text;
r[3] = Convert.ToInt32(((TextBox)(GridView1.FooterRow.FindControl("TextBox7"))).Text);
ds.Tables[0].Rows.Add(r);
ds.WriteXml(st);
getxml();
}
}

//Grid View Row Deleting Event to Delete From Xml File
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String st = Server.MapPath("emp.xml");
DataSet ds = new DataSet();
ds.ReadXml(st);
ds.Tables[0].Rows.RemoveAt(e.RowIndex);
ds.WriteXml(st);
getxml();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
getxml();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
getxml();
}

//Grid View row Updating Event to update xml file
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
String st = Server.MapPath("emp.xml");
DataSet ds = new DataSet();
ds.ReadXml(st);
ds.Tables[0].Rows[e.RowIndex][1] = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox2"))).Text;
ds.Tables[0].Rows[e.RowIndex][2] = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox4"))).Text;
ds.Tables[0].Rows[e.RowIndex][3] = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox6"))).Text);
GridView1.EditIndex = -1;
ds.WriteXml(st);
getxml();
}


Visit here for Source code

Reference http://howinaspnet.blogspot.com/2010/06/saveupdate-and-delete-in-xml-file-in.html
Related Resources:


Read related articles: Gridview operations    Gridview XML file    XML files    


Did you like this resource? Share it with your friends and show your love!


Responses to "Save,Update and Delete in Xml file in Asp.Net"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Adding new record in database from Gridview footer row
    Previous Resource: Grid view operations
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Xml  .  Gridview  .  Save update delete in xml using gridview  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.