Datasets in Microsoft.Net - Validating data in DataSet
(Page 3 of 8 )
Databases offer different mechanisms that you can use to ensure that the data in your database is valid. The sample Northwind database has many rules and constraints defined. The CustomerID column in the Customers table must be populated with a string of up to five characters, and that value must be unique within the table. The Orders table generates a new OrderID value for each row and requires that the CustomerID value for each row refer to an existing entry in the Customers table.
Sometimes you’ll want to apply similar rules to validate data in your application before submitting changes to your database. For example, let’s say you’re shopping on line and reach the page where you purchase the items in your basket. Most Web sites will make sure you’ve entered information into each of the required fields before they submit your order information to the appropriate database.
This type of logic might seem redundant because the database probably has similar validation rules defined. However, adding validation rules to your application can improve its performance. If a user fails to enter a credit card number, either by accident or in the hope that the system programmers were extremely lazy, the code for the Web page can easily determine that it can’t successfully submit the order without having to contact the database. The other benefits of this approach are a slight reduction of network traffic and a lighter load on your database.
The ADO.NET DataSet offers many of the same data validation mechanisms available in database systems. You can separate these validation mechanisms, also called constraints, into two categories—column-level restrictions and table-level restrictions.
Next: Validation Properties of the DataColumn >>
More ADO.NET Articles
More By Raghav Nayak