Export (0) Print
Expand All

DataTable.AcceptChanges Method

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

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

public void AcceptChanges()

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, and 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.

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.

private void AcceptOrReject(DataTable table)
{
    // If there are errors, try to reconcile. 
    if(table.HasErrors)
    { 
        if(Reconcile(table))
        {
            // Fixed all errors.
            table.AcceptChanges();
        }
        else
        {
            // Couldn'table fix all errors.
            table.RejectChanges();
        }
    }
    else 
        // If no errors, AcceptChanges.
        table.AcceptChanges();
}

private bool Reconcile(DataTable thisTable)
{
    foreach(DataRow row 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(row.HasErrors)
            return false;
    }
    return true;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft