Freigeben über


Walkthrough: Adding Validation to an N-Tier Data Application

Validating data is the process of confirming that the values entered into data objects (for example, DataTables or LINQ to SQL classes) comply with the constraints in an object's schema. Validation also ensures compliance with rules established for the application. Validating data before you send updates to the underlying database is a good practice that reduces errors. It also reduces the potential number of round trips between an application and the database.

This walkthrough provides step-by-step instructions for adding validation to an n-tier application (the solution created in the Walkthrough: Creating an N-Tier Data Application topic).

During this walkthrough, you will learn how to perform the following tasks:

Prerequisites

To complete this walkthrough, you need the following:

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.

Opening the NTierWalkthrough Solution

To open the NTierWalkthrough solution

Adding Validation to Check Data During an Individual Column Change

This procedure will add validation to verify that the OrderDate column in a new order is set to a value of the current date or earlier. The requirement of this application is that the OrderDate column cannot have a date that is later than today; future orders are not allowed.

To add validation to verify the value entered into the OrderDate column

  1. Open the dataset in the Creating and Editing Typed Datasets by double-clicking the NorthwindDataSet.xsd file in the DataAccessTier project in Solution Explorer.

  2. Double-click the OrderDate column of the Orders table in the designer. This action creates the ColumnChanging event handler.

    Note

    The Dataset Designer does not automatically create an event handler for the C# event. The code that is required to handle the event in C# is included later in this procedure. SampleColumnChangingEvent is created and then subscribes to the ColumnChanging event in the EndInit method.

  3. Add code to verify that e.ProposedValue for the OrderDate column contains the current date or earlier. If the proposed value is not valid, set the column to indicate that it contains an error.

    The following code example validates that the OrderDate column contains the current date or earlier. If the OrderDate value is a date later than today, the OrderDate column is set to an error. The Else clause clears the error if the OrderDate is today or earlier.

    Paste the Visual Basic code into the ColumnChanging event handler. Paste the C# code into the OrdersDataTable partial class declaration.

    If (e.Column.ColumnName = Me.OrderDateColumn.ColumnName) Then
        If CType(e.ProposedValue, DateTime) > Today Then
            e.Row.SetColumnError(e.Column, "OrderDate cannot be in the future")
        Else
            e.Row.SetColumnError(e.Column, "")
        End If
    End If
    
    // Replace the NorthwindDataSet partial class with the following:
    public partial class NorthwindDataSet
    {
        partial class OrdersDataTable
        {
            public override void EndInit()
            {
                base.EndInit();
                // Configure the ColumnChanging event
                // to call the SampleColumnChangingEvent method.
                ColumnChanging += SampleColumnChangingEvent;
            }
    
            public void SampleColumnChangingEvent(object sender, System.Data.DataColumnChangeEventArgs e)
            {
                if (e.Column.ColumnName == OrderDateColumn.ColumnName)
                {
                    if ((System.DateTime)e.ProposedValue > System.DateTime.Today)
                    {
                        e.Row.SetColumnError("OrderDate", " OrderDate cannot be in the future");
                    }
                    else
                    {
                        e.Row.SetColumnError("OrderDate", "");
                    }
                }
    
            }
        }
    }
    

Testing the Application

To test the application, change the value of the OrderDate column to a date that is not valid.

To test the application

  1. Press F5.

  2. The data from the Customers and Orders tables appears on the form.

  3. In the DataGridView that contains the orders, change the value in the OrderDate column (of any record) to tomorrow's date.

  4. Navigate off the row to accept the change.

    The validation fails and an error icon appears in the OrderDate cell that contains the invalid value.

  5. Rest the mouse pointer on the error icon to see the validation error.

  6. Close the form.

Next Steps

Depending on your application requirements, there are several steps that you may want to perform after you add validation to an n-tier application. For example, you might want to make the following enhancement to this application:

See Also

Other Resources

Working with Datasets in N-Tier Applications

Hierarchical Update

Accessing Data in Visual Studio