Walkthrough: Adding Validation to a Dataset
This walkthrough demonstrates how to validate data when changes are made to the data in a dataset. Where you perform validation checks on your data is dependent on the requirements of your application; for this walkthrough we validate data during changes to the values in individual columns. This walkthrough uses the ColumnChanging event to verify that an acceptable value is being entered into the record. If the value is not valid, an ErrorProvider control is displayed to the user.
The example also shows how to use the Dataset Designer to create a partial class for the dataset. (The partial class is where users can add code to extend the functionality of the Visual Studio–generated dataset. It is not be overwritten if the dataset is regenerated.)
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.
Tasks illustrated in this walkthrough include:
Creating a new Windows Application project.
Creating and configuring a dataset with the Data Source Configuration Wizard.
Selecting the control to be created on the form when dragging items from the Data Sources window. For more information, see How to: Set the Control to be Created when Dragging from the Data Sources Window.
Creating a data-bound control by dragging items from the Data Sources Window onto your form.
Creating a partial class to extend the functionality of the dataset.
Creating an event handler for the OrderDetails table's ColumnChanging event.
Adding validation to confirm that the Quantity column contains values greater than 0.
Displaying an ErrorProvider Component (Windows Forms) to inform users that a data-bound control contains invalid values.
Prerequisites
In order to complete this walkthrough, you need:
- Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
Creating a New Windows Application
To create the new Windows Application project
From the File menu, create a new project.
Name the project ValidationWalkthrough.
Select Windows Application and click OK. For more information, see Developing Client Applications with the .NET Framework.
The ValidationWalkthrough project is created and added to Solution Explorer.
Creating a New Data Source from Your Database
To create the data source
On the Data menu, click Show Data Sources.
In the Data Sources window, select Add New Data Source to start the Data Source Configuration Wizard.
Select Database on the Choose a Data Source Type page, and then click Next.
On the Choose your Data Connection page do one of the following:
If a data connection to the Northwind sample database is available in the drop-down list, select it.
-or-
Select New Connection to launch the Add/Modify Connection dialog box.
If your database requires a password, select the option to include sensitive data, and then click Next.
Click Next on the Save connection string to the Application Configuration file page.
Expand the Tables node on the Choose your Database Objects page.
Select the Order Details table, and then click Finish.
The NorthwindDataSet is added to your project and the OrderDetails table appears in the Data Sources window.
Creating Data-bound Controls
To create data bound controls on the form
In the Data Sources window, select the Order Details table.
Choose Details from the table's control list.
Drag the Order Details node from the Data Sources window onto Form1.
Data-bound controls with descriptive labels appear on the form, along with a tool strip (BindingNavigator) for navigating records. Data-bound controls with descriptive labels appear on the form, along with a tool strip (BindingNavigator) for navigating records. A NorthwindDataSet, Order_DetailsTableAdapter, BindingSource, and BindingNavigator appear in the component tray.
Adding an ErrorProvider Control to the Form
To configure an ErrorProvider control
Drag an ErrorProvider from the Toolbox onto Form1.
In the Properties window, set the ErrorProvider's DataSource property to the Order_DetailsBindingSource.
Note
Do not set the DataMember property.
Creating the ColumnChanging Event Handler
To create the validation event handlers
Open the NorthwindDataSet in the Dataset Designer by double-clicking the NorthwindDataSet.xsd file in Solution Explorer.
Double-click the Quantity column in the OrderDetails table to create the OrderDetailsDataTable_ColumnChanging event handler. (In C# only the data table's partial class will be created.)
Note
Double-clicking the table name (Order Details in the title bar) creates an event handler for the RowChanging event.
Add code to verify that e.ProposedValue contains values greater than 0. If the proposed value is 0 or less, set the column to indicate that it contains an error.
Paste the following code into the column-changing event handler below the Add user code here comment:
If CType(e.ProposedValue, Short) <= 0 Then e.Row.SetColumnError(e.Column, "Quantity must be greater than 0") Else e.Row.SetColumnError(e.Column, "") End If
// C# // Add the following code // to the partial class. public override void EndInit() { base.EndInit(); Order_DetailsRowChanging += TestRowChangeEvent; } public void TestRowChangeEvent(object sender, Order_DetailsRowChangeEvent e) { if ((short)e.Row.Quantity <= 0) { e.Row.SetColumnError("Quantity", "Quantity must be greater than 0"); } else { e.Row.SetColumnError("Quantity", ""); } }
Testing the Application
To test the application
Press F5 to run the application.
Change the value in the Quantity text box to 0.
Press TAB to move the focus out of the text box.
The error provider icon appears.
Rest your mouse pointer over the error provider to see the message.
Next Steps
Depending on your application requirements, there are several steps you may want to perform after adding validation. Some enhancements you could make to this walkthrough include:
Adding functionality to send updates back to the database. For more information, see Walkthrough: Saving Data to a Database (Single Table).
Editing the dataset to add or remove database objects. For more information, see How to: Edit a Dataset.
See Also
Concepts
Binding Windows Forms Controls to Data in Visual Studio
Preparing Your Application to Receive Data
Fetching Data into Your Application
Binding Controls to Data in Visual Studio
Editing Data in Your Application