Data Platform Developer Center > Data Platform Development Forums > ADO.NET DataSet > Change in DataTable.Clear() effect on new rows between Framework 1.1 and 2.0
Ask a questionAsk a question
 

AnswerChange in DataTable.Clear() effect on new rows between Framework 1.1 and 2.0

  • Thursday, April 26, 2007 7:13 PMDGMoffat Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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?

     

Answers

  • Saturday, April 28, 2007 12:10 AMCommonGenius.com Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Friday, April 27, 2007 11:44 PMPaul DomagMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

     

    Have you tried using the table.Rows.clear() instead?

     

     

     

     

    cheers,

     

    Paul June A. Domag

  • Saturday, April 28, 2007 12:10 AMCommonGenius.com Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

  • Tuesday, May 01, 2007 6:28 PMDGMoffat Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks for the input.

    We have done the rewrite and it now works in both versions.

  • Monday, October 26, 2009 2:34 PMGregDSmith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Tuesday, November 03, 2009 10:44 PMCommonGenius.com Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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