Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 307511 - Last Review: February 20, 2007 - Revision: 5.5
DOC: Clear() Method Throws an Exception When DataSet Is Bound to XmlDataDocument
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:
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
| kbbug kbdocerr kbnofix kbreadme KB307511 |
Retired KB Content DisclaimerThis 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
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please