This documentation is archived and is not being maintained.

DataTable.Clear Method

Clears the DataTable of all data.

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

Remarks

All rows in all tables are removed. An exception is generated if the table has any enforced child relations that would cause child rows to be stranded.

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 table of all data.

[Visual Basic] 
Private Sub ClearTable(myTable As DataTable)
    Try
        myTable.Clear()
    Catch e As DataException
    ' Process exception and return.
         Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
         log.Source = "My Application"
         log.WriteEntry(e.ToString())
         Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
    End Try
End Sub

[C#] 
private void ClearTable(DataTable myTable){
    try{
       myTable.Clear();
    }
    catch (DataException e){
    // Process exception and return.
        System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
        log.Source = "My Application";
        log.WriteEntry(e.ToString());
        Console.WriteLine("Exception of type {0} occurred.", e.GetType());
    }
    
 }

[C++] 
private:
void ClearTable(DataTable* myTable){
    try{
       myTable->Clear();
    }
    catch (DataException* e){
    // Process exception and return.
        System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
        log->Source = S"My Application";
        log->WriteEntry(e->ToString());
        Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
    }
    
 }

[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

DataTable Class | DataTable Members | System.Data Namespace

Show: