Microsoft Knowledge Base Email Alertz

The "DataSet.Clear Method" topic in the Microsoft .NET Framework Class Library documentation specifies that the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

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

Article ID: 307511 - View products that this article applies to.
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataSetClassClearTopic.asp

Properties

Article ID: 307511 - Last Review: February 20, 2007 - Revision: 5.5
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
Keywords: 
kbbug kbdocerr kbnofix kbreadme KB307511
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate