Turn off constraints while filling a dataset

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here

If a dataset contains constraints (such as foreign-key constraints),theycan raise errors related to the order of operations that are performed against the dataset. For example, loading child records before loadingrelated parent records can violate a constraint and cause an error. As soon as you load a child record, the constraint checks for the related parent record and raises an error.

If there were no mechanism to allow temporary constraint suspension, an error would be raised every time you tried to load a record into the child table. Another way to suspend all constraints in a dataset is with the BeginEdit, and EndEdit properties.

Note

Validation events (for example, ColumnChanging andRowChanging) will not be raised when constraints are turned off.

To suspend update constraints programmatically

  • The following example shows how to temporarily turn off constraint checking in a dataset:

    dataSet1.EnforceConstraints = false;
    // Perform some operations on the dataset
    dataSet1.EnforceConstraints = true;
    
    DataSet1.EnforceConstraints = False
    ' Perform some operations on the dataset
    DataSet1.EnforceConstraints = True
    

To suspend update constraints using the Dataset Designer

  1. Open your dataset in the Dataset Designer. For more information, see How to: Open a Dataset in the Dataset Designer.

  2. In the Properties window, set the EnforceConstraints property to false.

See Also

Fill datasets by using TableAdapters Relationships in datasets