ASP. NET data verification judgment and prompts at different stages
It is still a bit of trouble to perform data verification and prompt on the web.
1: In the page entry control, you can use the verification control
2: In the Dal (reference http://www.asp.net/learn/dataaccess/tutorial18cs.aspx? Tabid = 63)
Protected Void Gridview1_rowupdated ( Object Sender, gridviewupdatedeventargs e ){ If (E. Exception! = Null ){ // Display a user-friendly message Predictiondetails. Visible = True ; Exceptiondetails. Text =" There was a problem updating the product. ";If (E. Exception. innerexception! = Null ) {Exception inner = E. Exception. innerexception; If (Inner Is System. Data. Common. dbexception) exceptiondetails. Text + =" Our database is currently experiencing problems. Please try again later. "; Else If (Inner Is Nonullallowedexception) exceptiondetails. Text + =" There are one or more required fields that are missing. "; Else If (Inner Is Argumentexception ){ String Paramname = (argumentexception) Inner). paramname; predictiondetails. Text + = String . Concat (" The ", Paramname ," Value is illegal. ");} Else If (Inner Is Applicationexception) exceptiondetails. Text + = Inner. Message ;} // Indicate that the exception has been handled E. exceptionhandled = True ; // Keep the row in edit mode E. keepineditmode = True ;}}
3: In BLL
Public Bool Updateproduct ( String Productname, Decimal ? Unitprice, Short ? Unitsinstock, Int Productid) {northwind. productsdatatable products = Adapter. getproductbyproductid (productid ); If (Products. Count = 0) // No matching record found, return false Return False ; Northwind. productsrow Product = products [0]; // Make sure the price has not more than doubled If (Unitprice! = Null &&! Product. isunitpricenull ()) If (Unitprice> product. unitprice * 2) Throw New Applicationexception (" When updating a product price, "+" The new price cannot exceed twice the original price. "); Product. productname = productname;If (Unitprice = Null ) Product. setunitpricenull (); Else Product. unitprice = unitprice. value; If (Unitsinstock = Null ) Product. setunitsinstocknull (); Else Product. unitsinstock = unitsinstock. value; // Update the product record Int Rowsaffected = Adapter. Update (product ); // Return true if precisely one row was updated, otherwise false Return Rowsaffected = 1 ;}