Partager via


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:

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

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

ColumnChanging

The value in a column is being changed. The event passes the row and column to you along with the proposed new value.

ColumnChanged

The value in a column has been changed. The event passes the row and column to you along with the proposed value.

RowChanging

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.

RowChanged

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.

RowDeleting

A row is being deleted. The event passes the row to you and a value indicating what type of action (delete) is being performed.

RowDeleted

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

Concepts

Creating and Editing Typed Datasets