Change in DataTable.Clear() effect on new rows between Framework 1.1 and 2.0
We have a bit of code that sets the RowState for all rows in a DataTable to ADDED.
It does this by copying all the rows, deleting the old ones and adding the new ones to the table.
In 1.1 the code works fine.
In 2.0 it doesn't.
The code is:
private static void SetRowStateAdded(DataSet ds){
foreach (DataTable table in ds.Tables){
DataRow newRow = table.NewRow();
DataRow[] rows =
new DataRow[table.Select().Length];DataRow[] rowsTemp = table.Select();
for ( int i=0; i<rows.Length; i++ ){
rows[ i ] = table.NewRow();
rows[ i ].ItemArray = rowsTemp[ i ].ItemArray;
}
table.Clear();
foreach( DataRow row in rows ){
newRow = table.NewRow();
newRow.ItemArray = row.ItemArray;
// Copy a row.table.Rows.Add(newRow);
}
}
}
(I admit this is pretty bad code. We are rewriting it.)
The problem is that after the table.Clear() in 2.0 all the new DataRows in rows are cleared to DBNulls.
In 1.1 this did not happen and the rows could be put into the table successfully.
Is this a bug or a feature?
- Moved byVMazurMVP, ModeratorTuesday, November 03, 2009 11:34 AM (From:ADO.NET Data Providers)
Answers
Frankly I am surprised that worked in .NET 1.1. DataTable.Clear not only removes all of the data from the DataTable, it clears the schema as well. So adding a row to a cleared DataTable doesn't really make any sense. That's probably why they nulled out the values in .NET 2.0; adding a row created before the clear to the table after it is cleared is not a supported scenario anyway. Try using DataTable.Rows.Clear instead, to just clear the data but leave the schema.
And finish that rewrite as fast as you can

All Replies
Hi,
Have you tried using the table.Rows.clear() instead?
cheers,
Paul June A. Domag
Frankly I am surprised that worked in .NET 1.1. DataTable.Clear not only removes all of the data from the DataTable, it clears the schema as well. So adding a row to a cleared DataTable doesn't really make any sense. That's probably why they nulled out the values in .NET 2.0; adding a row created before the clear to the table after it is cleared is not a supported scenario anyway. Try using DataTable.Rows.Clear instead, to just clear the data but leave the schema.
And finish that rewrite as fast as you can

Thanks for the input.
We have done the rewrite and it now works in both versions.
- Where is it documented that the DataTable.Clear method also clears the table's schema? All of the documentation states that the method just removes all the rows from the table. The reason that I ask the question is because now I am concerned about what changes may have been made to the DataSet.Clear method. Does this method now remove all the rows from all tables and the schema from all tables?
- Greg,
It is not documented that DataTable.Clear clears the schema, and a quick test shows that is not actually the case. Not sure what gave me that impression in the first place.
Moderator | MCTS .NET 2.0 Web Applications | My Blog: http://www.commongenius.com


