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:
Automatically generate partial classes by using the Creating and Editing Typed Datasets.
Add code to perform validation when the value in an individual column changes.
Prerequisites
To complete this walkthrough, you need the following:
The solution and associated projects created in Walkthrough: Creating an N-Tier Data Application.
Access to the Northwind sample database. For more information, see How to: Install Sample Databases.
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
On the File menu, point to Open, click Project/Solution, and then navigate to the location of the NTierWalkthrough.sln file.
Note
The NTierWalkthrough.sln file is created in the Walkthrough: Creating an N-Tier Data Application topic and is a prerequisite to completing this walkthrough. Complete the Walkthrough: Creating an N-Tier Data Application topic, saving all projects when you are finished, if you have not yet done this.
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
Open the dataset in the Creating and Editing Typed Datasets by double-clicking the NorthwindDataSet.xsd file in the DataAccessTier project in Solution Explorer.
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.
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
Press F5.
The data from the Customers and Orders tables appears on the form.
In the DataGridView that contains the orders, change the value in the OrderDate column (of any record) to tomorrow's date.
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.
Rest the mouse pointer on the error icon to see the validation error.
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:
- Add validation that verifies changes when an entire record is changed. For information, see How to: Add Validation to an N-Tier Dataset.