Saturday 26 November 2011

Populating Grid Control from XML File


Populating Grid Control from XML File

In this article, I am going to teach you, how to populate grid view control using a XML File as Data Source.For using Customer.xml as data source, please refer my article “Populating Grid Control ,XML File and XSD File From Database”.

Procedure:-

  1. Add Customer.xml file using my article “Populating Data In XML And Grid” for populating grid control
  2. I have used ReadXml( ) to read the data content in my XML File.
  3. Server.MapPath( ) is used to get the exact location of the corresponding file.


Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Sample Program To Populate Grid Control Using XML File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="true"
            GridLines="None" onpageindexchanging="GridView1_PageIndexChanging">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>


Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=CTS;uid=sa;pwd=home;Initial Catalog=northwind");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadMyGridFromXML();
        }
    }

    public void loadMyGridFromXML()
    {
        conn.Open();
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("Customers.xml"));
        GridView1.DataSource = ds;
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        loadMyGridFromXML();
    }
}


Happy Coding.Explore more and more as possible.
To Download this article source code,click the below download link. For your convinience, I have attached Northwind Database script with this zip file.

Server1:  
Server2:

For more articles, follow my blog:-
http://dotnetwithmohan.blogspot.com

Populating Grid Control ,XML File and XSD File From Database


Populating Grid Control ,XML File and XSD File From Database

In this article, I am going to teach you,how to populate grid view control using a XML File as Data Source.
For using Customer.xml as data source, please refer my article “Populating Data In XML And Grid”.

Procedure:-

  1. I have used WriteXml( ) and WriteXMLSchema( ) to write the data content to my XML File and to write the data content to my XSD File respectively..
  2. Server.MapPath( ) is used to get the exact location of the corresponding file.



PopulateDataInXML.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Program Illustrating To Populate Data In Gridcontrol and In XML</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" AllowPaging="true"
            GridLines="None" onpageindexchanging="GridView1_PageIndexChanging">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

PopulateDataInXML.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=CTS;uid=sa;pwd=home;Initial Catalog=Northwind");
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadMyGridAndXML();
        }
    }

    public void loadMyGridAndXML()
    {
        conn.Open();
        SqlDataAdapter sda = new SqlDataAdapter("select * from customers",conn);
        DataSet data = new DataSet();
        sda.Fill(data, "customers");
        data.WriteXml(Server.MapPath("Customers.xml"));
  data.WriteXmlSchema(Server.MapPath("Customers.xsd"));            GridView1.DataSource = data;
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        loadMyGridAndXML();
    }
}

When you run the above PopulateDataInXML.aspx page, what you get is:-
1.GridView gets populated J
2.Customers.xml and Customers.xsd will be auto – coded since you use WriteXml() and WriteXmlSchema() functions. J

Your XML Output Looks Like This:-
Customers.xml :-
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <customers>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria Anders</ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Obere Str. 57</Address>
    <City>Berlin</City>
    <PostalCode>12209</PostalCode>
    <Country>Germany</Country>
    <Phone>030-0074321</Phone>
    <Fax>030-0076545</Fax>
  </customers>
  <customers>
    <CustomerID>ANATR</CustomerID>
    <CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
    <ContactName>Ana Trujillo</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>Avda. de la Constitución 2222</Address>
    <City>México D.F.</City>
    <PostalCode>05021</PostalCode>
    <Country>Mexico</Country>
    <Phone>(5) 555-4729</Phone>
    <Fax>(5) 555-3745</Fax>
  </customers>
 
// Since Customer table contains,91 records, similar to the above it appears..Each and every record will be placed inside a new <customers></customers> tag.
 
    <CustomerID>WOLZA</CustomerID>
    <CompanyName>Wolski  Zajazd</CompanyName>
    <ContactName>Zbyszek Piestrzeniewicz</ContactName>
    <ContactTitle>Owner</ContactTitle>
    <Address>ul. Filtrowa 68</Address>
    <City>Warszawa</City>
    <PostalCode>01-012</PostalCode>
    <Country>Poland</Country>
    <Phone>(26) 642-7012</Phone>
    <Fax>(26) 642-7012</Fax>
  </customers>
</NewDataSet>

Customers.xsd
<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="customers">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="CustomerID" type="xs:string" minOccurs="0" />
              <xs:element name="CompanyName" type="xs:string" minOccurs="0" />
              <xs:element name="ContactName" type="xs:string" minOccurs="0" />
              <xs:element name="ContactTitle" type="xs:string" minOccurs="0" />
              <xs:element name="Address" type="xs:string" minOccurs="0" />
              <xs:element name="City" type="xs:string" minOccurs="0" />
              <xs:element name="Region" type="xs:string" minOccurs="0" />
              <xs:element name="PostalCode" type="xs:string" minOccurs="0" />
              <xs:element name="Country" type="xs:string" minOccurs="0" />
              <xs:element name="Phone" type="xs:string" minOccurs="0" />
              <xs:element name="Fax" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>


Happy Coding.Explore more and more as possible.
To Download this article source code,click the below download link. For your convinience, I have attached Northwind Database script with this zip file.

For more articles, follow my blog:-
http://dotnetwithmohan.blogspot.com