connectseekers.com

Saturday, 9 August 2014

Gridview Export to XML in Asp.Net Using C#.Net

8/09/2014 - By Pranav Singh 0

This article will show you how you can export the gridview control to XML file using C#.net in asp.net. In this I will show you to export the file in a folder in the root directory present in folder.

So for this article first we will first we will create a new asp.net application and add the gridview and a button control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XMLExport.aspx.cs" Inherits="ProjectDemo_Asp.et.PDFExport" %>

<!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>GridView Gridview Export to XML in Asp.Net Using C#.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            BorderStyle="Solid" EmptyDataText="There are no data records to display.">
            <Columns>
                <asp:BoundField DataField="author_name" HeaderText="NAME" />
                <asp:BoundField DataField="publisher_name" HeaderText="PUBLISHER NAME" />
                <asp:BoundField DataField="publication_year" HeaderText="PUBLISH YEAR" />
                <asp:BoundField DataField="retail_price" HeaderText="PRICE" />
            </Columns>
            <HeaderStyle BackColor="#66CCFF" />
        </asp:GridView>
        <asp:Label ID="lblmessage" runat="server" style="color: #FF0000" Text=""></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Export To XML" />
   
    </div>
    </form>
</body>
</html>

Now we will check the code for exporting the gridview into xml.

using System;
using System.Web.UI;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;
namespace ProjectDemo_Asp.et
{
    public partial class PDFExport : System.Web.UI.Page
    {
        public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*Data to display on screen*/
                DataTable _objdt = new DataTable();
                _objdt = GetDataFromDataBase();
                if (_objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = _objdt;
                    GridView1.DataBind();
                }
            }
        }
        ///


        /// Function for binding retribing the data from database
        ///
        public DataTable GetDataFromDataBase()
        {
            DataTable _objdt = new DataTable();
            string querystring = "select * from Books;";
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
        ///


        /// Click event to export the grivview data into XML format
        ///
       protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string xmlFileName = "Student.xml";

                /*Data to bind to gridview and then export to CSV*/
                DataTable _objdt = new DataTable();
                _objdt = GetDataFromDataBase();
                if (_objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = _objdt;
                    GridView1.DataBind();
                }
                DataSet ds = new DataSet();
                DataTable dt = (DataTable)GridView1.DataSource;
                ds.Tables.Add(dt);
                ds.WriteXml(Server.MapPath("~/XML/" + xmlFileName));
                lblmessage.Text = "gridview exported successfully.";
            }
            catch (Exception ex)
            {
                lblmessage.Text = ex.Message.ToString();
            }

        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }
    }
}
        
In above code I have first bind the grid view code and then on button click event I have written code for export the code into xml. Now run the application and click on export button you will get the success message.


Now check the XML folder present int the application.


Now open the XML file to check the output.

DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top