This documentation is archived and is not being maintained.

DataTable.AcceptChanges Method

Commits all the changes made to this table since the last time AcceptChanges was called.

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

Remarks

When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged; Deleted rows are removed.

The AcceptChanges method is generally called on a DataTable after you attempt to update the DataSet using the DbDataAdapter.Update method.

Example

[Visual Basic, C#, C++] The following example tests each table for errors. If the table's errors can be reconciled (by passing it to an undefined function), AcceptChanges is called; otherwise, RejectChanges is called.

[Visual Basic] 
Private Sub AcceptOrReject(myTable As DataTable)
    ' If there are errors, try to reconcile.
    If( Not myTable.HasErrors) 
       If(Reconcile(myTable))
          ' Fixed all errors.
          myTable.AcceptChanges
       Else
         ' Couldn't fix all errors.
          myTable.RejectChanges
       End If
    Else
       ' If no errors, AcceptChanges.
       myTable.AcceptChanges()
    End If
 End Sub
 
Private Function Reconcile(thisTable As DataTable) As Boolean
    Dim myRow As DataRow
    For Each myRow in thisTable.Rows
       'Insert code to try to reconcile error.

       ' If there are still errors return immediately
       ' since the caller rejects all changes upon error.
       If myRow.HasErrors Then
           Reconcile = False
           Exit Function
       End If
    Next myRow
    Reconcile = True
 End Function

[C#] 
private void AcceptOrReject(DataTable myTable)
{
    // If there are errors, try to reconcile.
    if(!myTable.HasErrors)
    { 
       if(Reconcile(myTable))
       {
          // Fixed all errors.
          myTable.AcceptChanges();
       }
       else
       {
          // Couldn't fix all errors.
          myTable.RejectChanges();
       }
    }
    else
       // If no errors, AcceptChanges.
       myTable.AcceptChanges();
}
 
private bool Reconcile(DataTable thisTable)
{
    foreach(DataRow myRow in thisTable.Rows)
    {
       //Insert code to try to reconcile error.

       // If there are still errors return immediately
       // since the caller rejects all changes upon error.
       if(myRow.HasErrors)
           return false;
    }
    return true;
 }

[C++] 
private:
void AcceptOrReject(DataTable* myTable)
{
    // If there are errors, try to reconcile.
    if(!myTable->HasErrors)
    { 
       if(Reconcile(myTable))
       {
          // Fixed all errors.
          myTable->AcceptChanges();
       }
       else
       {
          // Couldn't fix all errors.
          myTable->RejectChanges();
       }
    }
    else
       // If no errors, AcceptChanges.
       myTable->AcceptChanges();
}
 
bool Reconcile(DataTable* thisTable)
{
    System::Collections::IEnumerator* myEnum = thisTable->Rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataRow* myRow = __try_cast<DataRow*>(myEnum->Current);
       //Insert code to try to reconcile error.

       // If there are still errors return immediately
       // since the caller rejects all changes upon error.
       if(myRow->HasErrors)
           return false;
    }
    return true;
 }

[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 | AcceptChanges | BeginEdit | DataRowState | EndEdit | RejectChanges

Show: