Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms
of Use |
DOC: Clear() Method Throws an Exception When DataSet Is Bound to XmlDataDocument
This article was previously published under Q307511
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
DataSet ds = new DataSet();
//Skip. Populate the DataSet.
//Bind the DataSet with XmlDataDocument.
XmlDataDocument docCust = new XmlDataDocument(ds);
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.
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++)
For more information, refer to the following .NET Framework
Class Library documentation:
Article ID: 307511 - Last Review: February 20, 2007 - Revision: 5.5
- Microsoft ADO.NET 1.0
- Microsoft ADO.NET 1.1
- Microsoft .NET Framework Class Libraries 1.0
- Microsoft .NET Framework Class Libraries 1.1
|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
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