Click here to Skip to main content
15,942,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Question:-
How to Load XML data and SQL table Data in GridView  Dynamically in ASP.NET


Answer:- 
Take Two Buttons then one GridView  and  one SQLDataSource


SQL
Go to Add New Item, select Dataset, Drag and Drop TableAdapter then
Establish the connection and Next, then choose Command Type Next, then Click on Query Builder
Select the Tables and link then (if more than one table you are selecting) then select the fields which all r u want to display


Now Add One XML File and write the fields which all u want to show


HTML
<!-- XML Table Data -->
<pre lang="xml"><bookstore>

  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>

  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>

  <book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>

  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>

</bookstore>


Now go for C# Coding 


C#
//On Click Button1 Load XML Table Data in GridView1
protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("XMLFile.xml"));

        DataSet ds = new DataSet();
        ds.ReadXml(new XmlNodeReader(doc));
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }

//On click Button2 Load SQL Table Data in Same Gridview1
protected void Button2_Click(object sender, EventArgs e)
    {
        DataSet1 ds1 = new DataSet1();

        DataSet1TableAdapters.DataTable1TableAdapter adap = new DataSet1TableAdapters.DataTable1TableAdapter();

        DataTable dt = new DataTable();
        adap.Fill(ds1.DataTable1);

        GridView1.DataSource = ds1.DataTable1;
        GridView1.DataBind();
    }
<pre lang="text"><pre lang="text">
Now in same Gridview we can show data from XML or SQL table
Onclick Button, Dynamically....

I hope It will help for Beginner of XML in ASP.NET
Md. Saud
Posted
Updated 12-Jan-12 1:28am
v2
Comments
thatraja 27-Dec-11 9:25am    
Not a question. Post it as a Tip/Trick
Mahid khan 10-Jan-15 2:20am    
i want both table load in gridview at same time
Md. Saud 28-Apr-15 2:39am    
as i have explained in Query Builder while selecting the tables you can have multiple tables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900