try
{
dtOne.Merge(dtTwo, false, MissingSchemaAction.Error);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Iterating DataTable using foreach loop:
Most of the time we need to iterate through the DataTable rows.
When using ASP.NET 1.X we used to write the following code to iterate through the DataTable.
private void UsingForEachLoop()
{
DataTable customerTable = GetDataTable();
foreach (DataRow row in customerTable.Rows)
{
Response.Write(row["Name"] as String);
Response.Write("<BR>");
}
}
Explanation of the Code:
The above code is pretty simple. I am using a simple foreach loop to iterate through the collection of rows which are contained inside the DataTable object.
Now let's see how the same thing can be done more efficiently using the ASP.NET 2.0.
private void IteratingDataTable()
{
DataTable customerTable = GetDataTable();
DataTableReader dtReader = new DataTableReader(customerTable);
while (dtReader.Read())
{
Response.Write( (string) dtReader["Name"]);
Response.Write("<BR>");
}
}
Explanation of the Code:
By looking at the code above the first thing that should come to your mind is the SqlDataReader object. And yes you are right, DataTableReader works pretty much the same way except that its disconnected from the database.
What is the advantage of using DataTableReader over foreach loop?
That's a good question to ask. And this can be explained by a simple example. Consider a situation in which you are iterating through the DataTable object and at one point you want to insert a new row in the DataTable. Writing code for this scenario is pretty straight forward. Check out the code below:
private void UsingForEachLoop()
{
DataTable customerTable = GetDataTable();
foreach (DataRow row in customerTable.Rows)
{
Response.Write(row["Name"] as String);
Response.Write("<BR>");
// Cannot add like this: Collection cannot be modified
AddRow(customerTable);
}
}
private void AddRow(DataTable dt)
{
if (counter == 0)
{
DataRow row = dt.NewRow();
row["Name"] = "AzamSharp";
dt.Rows.Add(row);
counter++;
}
}
The above code will throw an exception saying that Enumeration was alerted and it cannot be changed. This happens because you are adding the row to the DataTable while the iteration of the DataTable is in progress.
You can achieve this using DataTableReader object which let's you add a new row and magically it also knows that the new row has been added and hence it reads the new row.
private void IteratingDataTable()
{
DataTable customerTable = GetDataTable();
DataTableReader dtReader = new DataTableReader(customerTable);
while (dtReader.Read())
{
Response.Write( (string) dtReader["Name"]);
Response.Write("<BR>");
// AddRow simply adds a ONLY ONE ROW and ends
AddRow(customerTable);
}
}
private void AddRow(DataTable dt)
{
if (counter == 0)
{
DataRow row = dt.NewRow();
row["Name"] = "AzamSharp";
dt.Rows.Add(row);
counter++;
}
}
Serialization of DataSets:
For all those people who are passing DataSets through webservices or remoting the objects there is good news. DataSet class now include binary serialization which means that the data passed will be in the compressed and also stored in the compressed binary form. Take a look at the example below which uses xml as well as binary serialization techniques.
// Serialization Format
private void DataSetSerialization()
{
string xmlFileName = @"C:\ServerFolder\MyFile.xml";
string binaryFileName = @"C:\ServerFolder\MyFile.dat";
DataSet ds = GetDataSet();
ds.RemotingFormat = SerializationFormat.Binary;
IFormatter formatter = new BinaryFormatter();
using (Stream output = new FileStream(binaryFileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
// serialize dataset and write to disk
formatter.Serialize(output, ds);
}
}
DataSet class introduces a new property RemotingFormat which can be used to set up the type of Serialization Format. Its a better idea to use binary serialization when you have more rows like more than 100 in that case you will see the true benefit and the performance difference between the two types of serialization techniques.
I hope you liked the article, happy coding.
Attachments
Source Files DataSet in Asp .net 2.0