DOC: Clear() Method Throws an Exception When DataSet Is Bound to XmlDataDocument (307511)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1

This article was previously published under Q307511

SUMMARY

The "DataSet.Clear Method" topic in the Microsoft .NET Framework Class Library documentation specifies that the DataSet.Clear method "clears the DataSet of any data by removing all rows in all tables."

However, when a DataSet object is bound to an XmlDataDocument object, an exception is thrown if you call the Clear method on the tables.

For example, if you use the following code:
DataSet ds = new DataSet();

//Skip. Populate the DataSet. 
// ...

//Bind the DataSet with XmlDataDocument.
XmlDataDocument docCust = new XmlDataDocument(ds);			

ds.Tables.Clear();
				
the Clear method call raises the following exception:
System.InvalidOperationException: Cannot add or remove tables from the DataSet once the DataSet is mapped to a loaded XML document.

MORE INFORMATION

To remove the tables from the DataSet, you must traverse each table and remove each row one at a time. The following Microsoft Visual C# .NET code sample demonstrates this technique:
//row by row
foreach(DataTable dt in docCust.DataSet.Tables)
{
     int count = dt.Rows.Count; 
     for(int i=0; i<count; i++)
     {
       docCust.DataSet.Tables[dt.TableName].Rows.RemoveAt(0);
     }
}
				

REFERENCES

For more information, refer to the following .NET Framework Class Library documentation:

Modification Type:MinorLast Reviewed:4/25/2003
Keywords:kbbug kbdocerr kbnofix kbreadme KB307511