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.
- public partial class Northwind
- {
- public partial class ProductsDataTable
- {
- public override void BeginInit()
- {
- this.ColumnChanging += ValidateColumn;
- }
-
- void ValidateColumn(object sender, DataColumnChangeEventArgs e)
- {
- if(e.Column.Equals(this.UnitPriceColumn))
- {
- if(!Convert.IsDBNull(e.ProposedValue) && (decimal)e.ProposedValue < 0)
- {
- throw new ArgumentException("UnitPrice cannot be less than zero", "UnitPrice");
- }
- }
- else if (e.Column.Equals(this.UnitsInStockColumn) ||
- e.Column.Equals(this.UnitsOnOrderColumn) ||
- e.Column.Equals(this.ReorderLevelColumn))
- {
- if (!Convert.IsDBNull(e.ProposedValue) && (short)e.ProposedValue < 0)
- {
- throw new ArgumentException(string.Format("{0} cannot be less than zero", e.Column.ColumnName), e.Column.ColumnName);
- }
- }
- }
- }
- }
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.
- How to deploy the asp.net mvc program in IIS6.0
- Use Winform to build the asp.net mvc Framework
- Programming idea of ASP. NET Session failure
- ASP. NET Session state storage
- Understand ASP. NET Web Application Models