Microsoft KB Archive/307511

= DOC: Clear Method Throws an Exception When DataSet Is Bound to XmlDataDocument =

Article ID: 307511

Article Last Modified on 2/20/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 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 &quot;DataSet.Clear Method&quot; topic in the Microsoft .NET Framework Class Library documentation specifies that the DataSet.Clear method &quot;clears the DataSet of any data by removing all rows in all tables.&quot;

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