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

How to Merge DataSets using C#

Author: DevASP Team
Download Source Code : 706_Merge_DataSet.zip

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">

    <HeaderTemplate>

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

     </HeaderTemplate>

     <ItemTemplate>

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

      </ItemTemplate>

      <SeparatorTemplate>

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

       </SeparatorTemplate>

</asp:DataList>

 

 

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

  <Columns>                             

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

    <ItemTemplate>

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

     </ItemTemplate>               

     </asp:TemplateField>

   </Columns>

</asp:GridView>

 

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:

 

sconn.Close();

 

On the page load event add the following code:

 

        sconn.Open();

 

        DataSet ds = new DataSet();

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

        da.Fill(ds);       

 

        dlMergeDataSet.DataSource = ds;

        dlMergeDataSet.DataBind();

 

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

        DataSet ds1 = new DataSet();

        da.Fill(ds1);

 

        ds.Merge(ds1);

 

        GridView1.DataSource = ds;

        GridView1.DataBind();   

 

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 DevASP.net