Export (0) Print
Expand All

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

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

Tasks illustrated in this walkthrough include:

In order to complete this walkthrough, you need:

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.

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.

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.

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.

    NoteNote

    Do not set the DataMember property.

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

    NoteNote

    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", "");
            }
        }
    

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.

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:

Community Additions

ADD
Show:
© 2014 Microsoft