Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataSet.Clear Method

Clears the DataSet of any data by removing all rows in all tables.

[Visual Basic]
Public Sub Clear()
[C#]
public void Clear();
[C++]
public: void Clear();
[JScript]
public function Clear();

Remarks

If the DataSet is bound to an XmlDataDocument, calling DataSet.Clear or DataTable.Clear raises the NotSupportedException. To avoid this situation, traverse each table, removing each row one at a time.

Example

[Visual Basic, C#, C++] The following example clears the DataSet of all rows in all tables.

[Visual Basic] 
Private Sub ClearDataSet(ByVal myDataSet As DataSet)
   ' To test, print the number rows in each table.
  Dim t As DataTable
  For Each t In myDataSet.Tables
      Console.WriteLine(t.TableName & "Rows.Count = " & t.Rows.Count.ToString())
   Next
   ' Clear all rows of each table.
   myDataSet.Clear()
   ' Print the number of rows again.
  For Each t In myDataSet.Tables
      Console.WriteLine(t.TableName & "Rows.Count = " & t.Rows.Count.ToString())
   Next 
End Sub

[C#] 
private void ClearDataSet(DataSet myDataSet){
   // To test, print the number rows in each table.
   foreach(DataTable t in myDataSet.Tables){
      Console.WriteLine(t.TableName + "Rows.Count = " + t.Rows.Count.ToString());
   }
   // Clear all rows of each table.
   myDataSet.Clear();
   // Print the number of rows again.
   foreach(DataTable t in myDataSet.Tables){
      Console.WriteLine(t.TableName + "Rows.Count = "  + t.Rows.Count.ToString());
   }
}

[C++] 
private:
 void ClearDataSet(DataSet* myDataSet){
    // To test, print the number rows in each table.
    System::Collections::IEnumerator* myEnum = myDataSet->Tables->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataTable* t = __try_cast<DataTable*>(myEnum->Current);
       Console::WriteLine(S"{0}Rows->Count = {1}", t->TableName, __box(t->Rows->Count));
    }
    // Clear all rows of each table.
    myDataSet->Clear();
    // Print the number of rows again.
    System::Collections::IEnumerator* myEnum1 = myDataSet->Tables->GetEnumerator();
    while (myEnum1->MoveNext())
    {
       DataTable* t = __try_cast<DataTable*>(myEnum1->Current);
       Console::WriteLine(S"{0}Rows->Count = {1}", t->TableName, __box(t->Rows->Count));
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataSet Class | DataSet Members | System.Data Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft