Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to Merge DataSets using C#

Author: DevASP Team
Download Source Code :

With the help of this article you will learn how you can merge two datasets to one. This article will explain you the basic of it.

Sometimes the scenario came in development in which we want to merge the datasets or data tables to display similar record. For example you have two tables and you want to merge the rows of same columns so that it will display the record with combining effect. This technique is help full usually when you are using aliases columns.


Coming on to application drop two controls that are DataList control and GridView control. The code of your aspx will be look like as follows: (Note: you can apply styles to your controls as per your requirement).


<asp:DataList ID="dlMergeDataSet" runat="server" CellPadding="2" ForeColor="#333333">


      <asp:Label ID="lbLabel" runat="server" Font-Size="Small" Font-Names="Verdana" Font-Bold="true" Text="MIN SAL"> </asp:Label>



        <asp:Label ID="lbValue" runat="server" Font-Names="Verdana" Font-Size="Smaller" Text='<%# DataBinder.Eval(Container.DataItem, "SAL") %>'> </asp:Label>



          <hr size="1px" style="background-color: Black;" />





<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">


    <asp:TemplateField HeaderStyle-Font-Names="Verdana" HeaderText=" SAL">


        <asp:Label ID="lbValue2" runat="server" Font-Names="Verdana" Font-Size="Smaller" Text='<%# DataBinder.Eval(Container.DataItem, "SAL") %>'> </asp:Label>






Now in aspx.cs file import the System.Data.SqlClient by using “Using” command. Form a connection string:


SqlConnection sconn = new SqlConnection("User ID=ID;Password=pwd;Initial Catalog=DEMODB;Data Source=HOME");


Note: declare this connection string globally so that connection object is accessible in whole of your partial class.


Now on the page load first register the Page_Unload event and in this event add the following line this will close the DB connection on page unload:




On the page load event add the following code:




        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("SELECT LOSAL AS SAL FROM SALGRADE", sconn);



        dlMergeDataSet.DataSource = ds;



        da = new SqlDataAdapter("SELECT HISAL AS SAL FROM SALGRADE", sconn);

        DataSet ds1 = new DataSet();





        GridView1.DataSource = ds;



On the execution of first command you will see that LOSAL column is retrieved this record set is bind with Datalist control. In second execution of command the HISAL column is get and the pervious dataset is merged with new dataset and finally it is bind with GridView DataSource. Here you have the two different data columns that are merged to one dataset using SAL alise.

Article Comments
Thank you for this useful article.

Posted on 12/10/2007 9:33:41 PM by kittiphong

gud one!!!!

Posted on 12/14/2009 1:18:40 AM by nishant

Very good , thanks for this simple article

Posted on 7/26/2010 6:59:59 AM by Abhijit Mane

Add Article Comment:
Name :
Email Address :
Comments :
<< How to Sort Grid View Columns Programmatically

Disclaimer - Privacy
© 2002-2017