This documentation is archived and is not being maintained.

Data Validation in Datasets

You can confirm that data being written to a dataset is valid by building validation checks into the dataset itself. The dataset can check the data no matter how the update is being performed — whether directly by controls in a form, within a component, or in some other way. Because the dataset is part of your application, it is a logical place to build application-specific validation (unlike building the same checks into the database backend). Validation within a dataset can be accomplished:

  • By creating keys, unique constraints, and so on as part of the actual schema definition of the dataset. For more information on incorporating validation into the schema definition, see Creating Primary and Unique Keys in XML Schemas.
  • By creating your own application-specific validation that can check data during column and row changing events.
  • By setting the DataColumn object's properties, such as MaxLength, AllowDBNull, and Unique.

There are several events that are raised by the DataTable object when a change is occurring in a record:

  • The ColumnChanging and ColumnChanged events are raised during and after each change to an individual column. The ColumnChanging event is useful when you want to validate changes in specific columns. Information about the proposed change is passed as an argument with the event. For more information see Validating Data During Column Changes.
  • The RowChanging and RowChanged events are raised during and after any change in a row. The RowChanging event is more general, in that it simply indicates that a change is occurring somewhere in the row; you do not know which column has changed. For more information see Validating Data During Row Changes.

By default, each change to a column therefore raises four events: first the ColumnChanging and ColumnChanged events for the specific column being changed, and then the RowChanging and RowChanged event. If multiple changes are being made to the row, the events will be raised for each change.

Note   The data row's BeginEdit method turns off the RowChanging and RowChanged events after each individual column change. In that case, the event is not raised until the EndEdit method has been called, when the RowChanging and RowChanged events are raised just once. For more information, see Suspending Update Constraints.

The event you choose depends on how granular you want the validation to be. If it is important that you catch an error immediately when a column is changed, build validation using the ColumnChanging event. Otherwise, use the RowChanging event, which might result in catching several errors at once. Additionally, if your data is structured in such a way that the value of one column is validated based on the contents of another column then you should perform your validation during the RowChanging event.

See Also

Dataset Updates in Visual Studio .NET | Introduction to Validating User Input in Web Forms | Validating Data During Column Changes | Validating Data During Row Changes | DataTable.ColumnChanging Event | DataTable.RowChanging Event | DataColumn Properties