DataAdapter.AcceptChangesDuringUpdate Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets whether AcceptChanges is called during a Update.

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

public bool AcceptChangesDuringUpdate { get; set; }

Property Value

Type: System.Boolean

true if AcceptChanges is called during an Update; otherwise false. The default is true.

During a call to the Update method of a DataAdapter, the database can send data back to your ADO.NET application as output parameters or as the first returned record of a result set. ADO.NET can retrieve these values and update the corresponding columns in the DataRow being updated. By default, ADO.NET calls the AcceptChanges method of the DataRow after the update. However, if you want to merge the updated row back into another DataTable, you may want to preserver the original value of a primary key column. For example, a primary key column corresponding to an automatically incrementing column in the database, such as an identity column, can contain new values that are assigned by the database that do not match the original values assigned in the DataRow. By default, AcceptChanges is called implicitly after an update, and the original values in the row, which may have been AutoIncrement values assigned by ADO.NET, are lost. You can preserve the original values in the DataRow by preventing ADO.NET from calling AcceptChanges after it performs an update on a row, by setting the AcceptChangesDuringUpdate property to false, which preserves the original values.


Setting the AcceptChangesDuringUpdate property to false applies to all data modifications, not only inserts. If you want to edit or delete rows in the same update, and if you want to suppress the call to AcceptChanges only for inserts, then instead of setting AccceptChangesDuringUpdate to false, use an event handler for the RowUpdated event of the DataAdapter. In the event handler you can check the StatementType to determine if the data modification is an insert, and if true, set the Status property of the RowUpdatedEventArgs to SkipCurrentRow. For more information and an example, see Retrieving Identity or Autonumber Values.

This example demonstrates extracting changed rows from a DataTable and using a SqlDataAdapter to update the data source and retrieve a new identity column value. By setting the AcceptChangesDuringUpdate property of the SqlDataAdapter to false to preserve the original auto increment value, the new data can then be merged into the original DataTable, even if the new identity value does not match the original auto increment value in the DataTable.

private static void MergeIdentityColumns(string connectionString)
    using (SqlConnection connection =
               new SqlConnection(connectionString))
        // Create the DataAdapter
        SqlDataAdapter adapter =
            new SqlDataAdapter(
            "SELECT ShipperID, CompanyName FROM dbo.Shippers",

        //Add the InsertCommand to retrieve new identity value.
        adapter.InsertCommand = new SqlCommand(
            "INSERT INTO dbo.Shippers (CompanyName) " +
            "VALUES (@CompanyName); " +
            "SELECT ShipperID, CompanyName FROM dbo.Shippers " +
            "WHERE ShipperID = SCOPE_IDENTITY();", connection);

        // Set AcceptChangesDuringUpdate to false
        adapter.AcceptChangesDuringUpdate = false;

        // Add the parameter for the inserted value.
           new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40,
        adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;

        // MissingSchemaAction adds any missing schema to 
        // the DataTable, including auto increment columns
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

        // Fill a DataTable.
        DataTable shipper = new DataTable();

        // Add a new shipper row. 
        DataRow newRow = shipper.NewRow();
        newRow["CompanyName"] = "New Shipper";

        // Add changed rows to a new DataTable. This
        // DataTable will be used to update the data source.
        DataTable dataChanges = shipper.GetChanges();


        Console.WriteLine("Rows after merge.");
        foreach (DataRow rowBefore in shipper.Rows)
                Console.WriteLine("{0}: {1}", rowBefore[0], rowBefore[1]);

        // Merge the two DataTables to get new values.

        // Commit the changes.

        Console.WriteLine("Rows after merge.");
        foreach (DataRow rowAfter in shipper.Rows)
                Console.WriteLine("{0}: {1}", rowAfter[0], rowAfter[1]);

.NET Framework
Available since 2.0
Return to top