Share via


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 Visual Studio Settings.

Tasks illustrated in this walkthrough include:

Prerequisites

In order to complete this walkthrough, you need:

Creating a New Windows Application

To create the new Windows Application project

  1. From the File menu, create a new project.

  2. Name the project ValidationWalkthrough.

  3. Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.

    The ValidationWalkthrough project is created and added to Solution Explorer.

Creating a New Data Source from Your Database

To create the data source

  1. On the Data menu, click Show Data Sources.

  2. In the Data Sources window, select Add New Data Source to start the Data Source Configuration Wizard.

  3. Select Database on the Choose a Data Source Type page, and then click Next.

  4. 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. For more information, see Add/Modify Connection Dialog Box (General).

  5. If your database requires a password, select the option to include sensitive data, and then click Next.

  6. Click Next on the Save connection string to the Application Configuration file page.

  7. Expand the Tables node on the Choose your Database Objects page.

  8. 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

  1. In the Data Sources window, select the Order Details table.

  2. Choose Details from the table's control list.

  3. 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

  1. Drag an ErrorProvider from the Toolbox onto Form1.

  2. 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

  1. Open the NorthwindDataSet in the Dataset Designer by double-clicking the NorthwindDataSet.xsd file in Solution Explorer.

  2. 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.

  3. 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

  1. Press F5 to run the application.

  2. Change the value in the Quantity text box to 0.

  3. Press TAB to move the focus out of the text box.

    The error provider icon appears.

  4. 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:

See Also

Concepts

What's New in Data Application Development

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

Validating Data

Saving Data

Other Resources

Data Walkthroughs

Connecting to Data in Visual Studio