Validating Data in Datasets
Validating data is the process of confirming that the values being entered into data objects conform to the constraints within a dataset's schema, as well as the rules established for your application. Validating data prior to sending updates to the underlying database is a good practice that reduces errors as well as the potential number of round trips between an application and the database. 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).
The suggested location to add validation into your application is the dataset's partial class file. In Visual Basic or Visual C#, open the DataSet Designer and double-click the column or table you want to create validation for. This action automatically creates a ColumnChanging or RowChanging event handler. For more information, see How to: Validate Data During Column Changes, or How to: Validate Data During Row Changes. For a complete example, see Walkthrough: Adding Validation to a Dataset.
Validating Data
Validation within a dataset can be accomplished:
By creating your own application-specific validation that can check data during changes to values in an individual data column. For more information, see How to: Validate Data During Column Changes.
By creating your own application-specific validation that can check data during changes to values while an entire data row is changing. For more information, see How to: Validate Data During Row Changes.
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 Constraining a DataColumn to Contain Unique Values.
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 How to: Validate 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 How to: Validate 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 How to: Turn Off Constraints While Filling a Dataset.
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.
When records are updated, the DataTable object raises events that you can respond to as changes are occurring and after changes are made.
If your application is using a typed dataset, you can create strongly typed event handlers. This will add four additional typed events that you can create handlers for; dataTableNameRowChanging, dataTableNameRowChanged, dataTableNameRowDeleting, and dataTableNameRowDeleted. These typed event handlers pass an argument that includes the column names of your table that make code that easier to write and read.
Data Update Events
Event |
Description |
---|---|
The value in a column is being changed. The event passes the row and column to you along with the proposed new value. |
|
The value in a column has been changed. The event passes the row and column to you along with the proposed value. |
|
Changes made to a DataRow object are about to be committed back into the dataset. If you have not called the BeginEdit method, the RowChanging event is raised for each change to a column, immediately after the ColumnChanging event has been raised. If you did call BeginEdit before making changes, the RowChanging event is raised only when you call the EndEdit method. The event passes the row to you and a value indicating what type of action (change, insert, and so on) is being performed. |
|
A row has been changed. The event passes the row to you and a value indicating what type of action (change, insert, and so on) is being performed. |
|
A row is being deleted. The event passes the row to you and a value indicating what type of action (delete) is being performed. |
|
A row has been deleted. The event passes the row to you and a value indicating what type of action (delete) is being performed. |
The ColumnChanging, RowChanging, and RowDeleting events are raised during the update process. You can use these events to validate data or perform other types of processing. Because the updates are in process during these events, you can cancel the update by throwing an exception, which prevents the change from being completed.
The ColumnChanged, RowChanged, and RowDeleted events are notification events that are raised when the update has been completed successfully. These events are useful when you want to take further action based on a successful update.
See Also
Tasks
How to: Connect to Data in a Database
How to: Validate Data in the Windows Forms DataGridView Control
How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component