Introduction:
We all have been using DataSet object to in one way or the other.
The most common use of the DataSet object is to populate web server controls. These controls usually include template server controls like Datagrid, DataList and Repeater. ASP.NET 2.0 comes with several new features for the DataSet, DataTable classes. In this article we will go over some of the new features of the DataSet and DataTables and how these features will benefit the developer to build more robust applications.
Merging Two DataTables:
Consider a situation in which you have two DataTable objects and you need to merge them. If you are using ASP.NET 1.X then this can be accomplished using the following code:
// This method merges the two tables
private void MergeDataTable()
{
DataTable dtOne = new DataTable("FirstTable");
dtOne.Columns.Add("MyColumn");
DataTable dtTwo = new DataTable("SecondTable");
dtTwo.Columns.Add("MyColumn");
for(int i=1;i<=10;i++)
{
DataRow row = dtOne.NewRow();
row["MyColumn"] = "Data"+i;
dtOne.Rows.Add(row);
}
for(int i=11;i<=20;i++)
{
DataRow row = dtTwo.NewRow();
row["MyColumn"] = "Data"+i;
dtTwo.Rows.Add(row);
}
DataSet ds = new DataSet();
ds.Tables.Add(dtOne);
ds.Merge(dtTwo);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
Explanation of the Code:
In the code above I simply make an object of the DataTable class. Add one column to the DataTable and then ran a loop to populate the DataTable with some dummy data. I followed the same steps to create and populate the second DataTable. After populating the DataTables my next task is to merge both tables into one. In ASP.NET 1.X you achieve this by using a DataSet as the container and adding both one table to the DataSet and then using the merge method to merge another DataTable to the DataSet.
If you bind this DataSet to the DataGrid you will only see the contents of the first DataTable which in this case is dtOne. The reason is simple by default DataSet is set to the first table in its collection which in our case is dtOne.
Now, let's see that how the same task of merging can be done when using the enhanced ASP.NET 2.0 DataSet object.
private void MergeDataTable()
{
DataTable dtOne = new DataTable("MyFirstTable");
dtOne.Columns.Add("Number");
DataTable dtTwo = new DataTable("MySecondTable");
dtTwo.Columns.Add("Number");
// Populate the first table
for (int i = 1; i <= 10; i++)
{
DataRow row = dtOne.NewRow();
row["Number"] = "Data" + i;
dtOne.Rows.Add(row);
}
// Populate the second table
for (int i = 11; i <= 20; i++)
{
DataRow row = dtTwo.NewRow();
row["Number"] = "Data" + i;
dtTwo.Rows.Add(row);
}
// Now merge the two tables /* ONLY ONE LINE OF CODE COOL RIGHT! */
dtOne.Merge(dtTwo);
GridView1.DataSource = dtOne;
GridView1.DataBind();
}
Explanation of the Code:
The code is pretty much the same as I did before the only difference is the third last line where I used DataTables merge method to merge another table. Merge method is a new method that is introduced in ASP.NET 2.0 DataSet object.
Merging Two DataTables with Different Schema:
I know you must be wondering that what will happen if you try to merge two DataTables with different schema. Well you can try it out and you will find out that it does not throw any exception. The columns that does not belong to other DataTables will be left blank and hence no entry will be made in those columns. Check out the code below to get the clear idea of merging DataTables with different schemas.
private void MergeWithDifferentSchema()
{
DataTable dtOne = new DataTable("MyFirstTable");
dtOne.Columns.Add("Name");
dtOne.Columns.Add("Phone");
DataTable dtTwo = new DataTable("MySecondTable");
dtTwo.Columns.Add("Name");
dtTwo.Columns.Add("Email");
for (int i = 1; i <= 10; i++)
{
DataRow row = dtOne.NewRow();
row["Name"] = "Customer" + i;
row["Phone"] = "Phone" + i;
dtOne.Rows.Add(row);
}
for (int i = 10; i <= 20; i++)
{
DataRow row = dtTwo.NewRow();
row["Name"] = "Customer" + i;
row["Email"] = "Email" + i;
dtTwo.Rows.Add(row);
}
dtOne.Merge(dtTwo);
GridView1.DataSource = dtOne;
GridView1.DataBind();
}
Explanation of the Code:
In the above code I am making two DataTables each having a different schema. This means that DataTable dtOne has different columns from DataTable dtTwo. The merge is performed the same way using the Merge method of the DataTable object. The result is shown in the screen shot below:
You can easily configure it so if two DataTables of different schemas are merged together an error will be generated.
Attachments
Source Files DataSet in Asp .net 2.0