ASP. NET 2.0 data Tutorial: add field-level verification to DataRow

Source: Internet
Author: User

Adding field-level verification to DataRow is to check all attribute values involved in the business object during insertion or update. For example, some field-level verification rules are as follows:

· The ProductName field cannot exceed 40 characters

· The QuantityPerUnit field cannot exceed 20 characters

· The ProductID, ProductName, and Discontinued fields are required, while other fields are optional.

· The UnitPrice, UnitsInStock, UnitsOnOrder, and ReorderLevel fields must not be less than 0.

These rules can or should be described at the database layer. The maximum number of characters in the ProductName and QuantityPerUnit fields can be nvarchar (40) and nvarchar (20) by using the data type of the corresponding columns in the Products table )). You can set the corresponding column of the table in the database to "allowed to be NULL" for the field "required. To ensure that the values of the UnitPrice, UnitsInStock, UnitsOnOrder, and ReorderLevel fields are not less than 0, you can add a constraint to the corresponding columns respectively.

In addition to applying these rules to databases, these rules will also be applied to DataSet. In fact, the field length and whether to allow null information have been applied to the DataColumn set of each able. We can see the existing field-level verification in the DataSet Designer), select a field from a DataTable, and then find it in the Properties window. 4. The QuantityPerUnit field in ProductDataTable allows null values and the maximum length is 20 characters. If we try to set a string with a length greater than 20 characters for the QuantityPerUnit attribute of a ProductsDataRow, an ArgumentException will be thrown.

 

Figure 4: DataColumn provides basic field-level verification

Unfortunately, we cannot specify a boundary check through the attribute window. For example, the value of UnitPrice cannot be less than 0. To provide such field-level verification, we need to create an Event Handler for the ColumnChanging Event of the DataTable. As mentioned in the previous tutorial, DataSet, able, and DataRow objects created from typed datasets can be extended using the partial class. With this technology, we can create a ColumnChanging Event Handler for ProductDataTable. Create a new class file named ProductsDataTable. ColumnChanging. cs in the App_Code folder, as shown in.

 

Figure 5: Add a new class in the App_Code folder

Create an Event handler for the ColumnChanging Event to ensure that the values of the UnitPrice, UnitsInStock, UnitsOnOrder, and ReorderLevel fields are not less than 0. If the values of these columns are out of the range, an ArgumentException is thrown.

  
 
  1. public partial class Northwind    
  2. {    
  3.     public partial class ProductsDataTable    
  4.     {    
  5.         public override void BeginInit()    
  6.          {    
  7.             this.ColumnChanging += ValidateColumn;    
  8.          }    
  9.    
  10.          void ValidateColumn(object sender, DataColumnChangeEventArgs e)    
  11.          {    
  12.             if(e.Column.Equals(this.UnitPriceColumn))    
  13.             {    
  14.                if(!Convert.IsDBNull(e.ProposedValue) && (decimal)e.ProposedValue <  0)    
  15.                {    
  16.                   throw new ArgumentException("UnitPrice cannot be less than zero", "UnitPrice");    
  17.                }    
  18.             }    
  19.             else if (e.Column.Equals(this.UnitsInStockColumn) ||    
  20.                     e.Column.Equals(this.UnitsOnOrderColumn) ||    
  21.                    e.Column.Equals(this.ReorderLevelColumn))    
  22.             {    
  23.                 if (!Convert.IsDBNull(e.ProposedValue) && (short)e.ProposedValue <  0)    
  24.                 {    
  25.                     throw new ArgumentException(string.Format("{0} cannot be less than zero", e.Column.ColumnName), e.Column.ColumnName);    
  26.                 }    
  27.             }    
  28.          }    
  29.     }    
  30. }   

In this way, the third step of the business logic layer in asp.net 2.0 data operations is implemented: add field-level verification to DataRow.

  1. How to deploy the asp.net mvc program in IIS6.0
  2. Use Winform to build the asp.net mvc Framework
  3. Programming idea of ASP. NET Session failure
  4. ASP. NET Session state storage
  5. Understand ASP. NET Web Application Models
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.