1 / 4
2 / 4
3 / 4
4 / 4

Thursday 12 March 2020

Create XML format of your existing Database Table and Show on GridView in Asp.Net C#


In this article, we are going to cover two topics i.e. how to Generate a .xml file in our project and Get that XML file data into a Gridview.


Sometimes in real-time work, we have to maintain some value(The value is not changing frequently) to display on our websites so many times. So we need to connect that much of time with the database and fetch it again and again.

For Example:- We are maintaining a list of  Govt. schools and It's basic Details of our state. This detail is not going to change frequently. It may change when Govt will declare any school as Govt. Schools or new schools will be added, and this will happen very rarely in a year or may take many years. 
In this case, if we are getting the data from Database in every request then it can increase the extra load on the server and slower the loading time.

So to speed up the loading and to reduce the extra burden on the server we can maintain an XML file for that database table. Here we can get the data from the database for once and load it to XML then we can retrieve the data from XML without connecting to the database again and again.

Let's start

Step1:-create a database table "tblUser".insert some records into that table.



Step3:-Add a Folder "XMLMaster" to your project






Step3:-Add a webform





>>Code under .aspx file <body>  tag



  <form id="form1" runat="server">
        <div>  
           <asp:Button ID="ButtonGenerateXml" runat="server" OnClick="btnGenxml_Click" Text="Generate Xml" />
            <br />
            <asp:Label ID="LabelMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
            <br />
            <asp:Button ID="ButtonGetXml" runat="server" OnClick="btnGetxml_Click" Text="Bind Grid From XML" />
            <br />
            <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" style="text-align: left" Width="440px">
                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                <RowStyle BackColor="White" ForeColor="#003399" />
                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                <SortedDescendingHeaderStyle BackColor="#002876" />

            </asp:GridView>
    </div>
 </form>

>>Code under .aspx.cs 

//namescapces

using System;
using System.Data;
using System.IO;
using System.Data.SqlClient;

//code under class

SqlConnection con=new SqlConnection("Data Source=(local); uid=sa; pwd=1234;database=Demo;");
protected void ButtonGenerateXml_Click(object sender, EventArgs e)
        {
            string strSQL = "Select * from tblUser";
            SqlDataAdapter sda = new SqlDataAdapter(strSQL, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            ds.WriteXml(Server.MapPath("~/XMLMaster/Xml_file.xml"));
            LabelMessage.Text = "<a href=../XMLMaster/Xml_file.xml>View XML file</a>";
        }

        protected void ButtonGetXml_Click(object sender, EventArgs e)
        { 
            DataSet ds = new DataSet();
            if (File.Exists(Server.MapPath("~/XMLMaster/Xml_file.xml")))
            {
                ds.ReadXml(Server.MapPath("~/XMLMaster/Xml_file.xml"));
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }

On the click of the button "btnGenxml", you will get an XML file link..there you can see the XML file that is generated from my database table"tblUser".
On the click of button "btnGetxml", you will get the data from the XML to your Gridview "Grid1".












No comments:

Post a Comment

If you have any doubts please let me know