Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Generating XML File From SQL Database Using C#

 by Rohatash Kumar on Jan 31, 2012

In this blog, we Generate XML File From SQL Database Using C#.
Comments: 0 Views: 766 Printable Version 

In this blog, we Generate XML File From SQL Database Using C#.

Creating Table in SQL Server Database

Now create a table named UserDetail with the columns UserId and UserName. The table looks like as below.

img1.jpg

Now insert some data into the table.

mg4.jpg

Now creating a new web application project in Visual Studio 2010. Now add the following namespace.

using System.Data.SqlClient;

using System.Data;

using System.Xml;


Now write the connection string to connect to the database.

 

string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";

Let's take a closer look at using Generating XML files from SQL Database within the .NET Framework. Creating a new web application project in Visual Studio 2010. Drag and Drop a Button and Label control to the form and make a link in Label control to show the XML file. For a sample design your aspx page might be like this.

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />

<asp:Label ID="LabelMessage" runat="server" ForeColor ="Red" Text="ShowMessage"></asp:Label>

 

In Codebehind write the following code on the Button Event. 

Codebehind 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

using System.Xml;

namespace WebApplication119

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");

            string strSQL = "Select UserId, UserName from UserDetail";

            SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);

            DataSet ds = new DataSet();

            dt.Fill (ds,"UserDetail");

            ds.WriteXml(Server.MapPath("emp_xml_file.xml"));

           LabelMessage.Text = "<a href=emp_xml_file.xml> XML file</a>";

 

        }    

    }

}

 

Now run the application and test it.

mg1.jpg

Now click on the Button link will be generate to show the XML file.

mg2.jpg

Now click on the link to show the XML file.

mg3.jpg

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
Team Foundation Server Hosting
Become a Sponsor

 Blogger's Profile
Age: Not Available
Location:
Title: Developer
Joined: Aug 10, 2010
Education: Masters Degree
 More Blogs from this Blogger
No record available
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.