Suspending Update Constraints
If a dataset contains constraints (such as a foreign-key constraint), updating a column in a record can violate the constraint: After you have finished updating one column but before you get to the next one, the record can temporarily be in an error state. For example, imagine that you have a data table in your dataset with two columns that do not allow null values. As soon as you start to write a new record there will always be a null value in at least one of the columns (you can only write one column at a time). If there were no mechanism to allow temporary constraint suspension, an error would be raised every time after writing the first column and before writing the second.
To suspend update constraints
- Before changing data in a data row, call the DataRow object's BeginEdit method.
Note Changes you make to the row after calling BeginEdit are saved.
- Make the updates to that row.
- Call the EndEdit method to commit changes to the row and re-enable constraint checking.
The RowChanging event is raised.
- Call the row's CancelEdit method to discard changes to the row.
The following example shows a simple update that is bracketed with BeginEdit and EndEdit method calls. This example assumes that you have a table called
Customersin a typed dataset called
' Visual Basic DsCustomers1.Customers(4).BeginEdit() DsCustomers1.Customers(4).CompanyName = "Wingtip Toys" DsCustomers1.Customers(4).City = "Buffalo" DsCustomers1.Customers(4).EndEdit() // C# dsCustomers1.Customers.BeginEdit(); dsCustomers1.Customers.CompanyName = "Wingtip Toys"; dsCustomers1.Customers.City = "Buffalo"; dsCustomers1.Customers.EndEdit();
Introduction to Dataset Updates | Updating, Inserting, and Deleting Records in a Dataset | Data Update Events | BeginEdit | EndEdit | CancelEdit | RowChanging Event | Code: Turning Off Constraints While Filling a DataSet (Visual Basic)