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).
Now in aspx.cs file import the System.Data.SqlClient by using “Using” command. Form a connection string:
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:
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.