| Home | Handling Database Errors | KV's Kool ASP | |||||||||||||||||||||||||||
|
Though we put lot of effort for testing and debugging some times we will face error messages like... Microsoft OLE DB Provider for ODBC Drivers error '80004005'[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified /pkvnm/samples/errorNormal.asp, line 9 Naturally these messages make the visitor run away from the site and we lose the credibility. Instead, let us show them an error message like... Sorry, due to technical problems, I cannot process your request now. But how? ASP has an error object called Err with global scope which will trap all the runtime errors occured while executing the scripts. There is no need to create an instance of Err object in your code. The properties of the Err object are set when an error occurs. The default property of the Err object is Number. Err.Number contains an integer. When a run-time error occurs, the properties of the Err object are filled with information that uniquely identifies the error and information that can be used to handle it. To generate a run-time error in your code, use the Raise method. On Error Resume Next statement instructs the ASP not to stop processing when a run time error occurs and continue to execute from the next line where the error occurs. The Err object's properties are reset to zero or blank strings ("") after an On Error Resume Next statement. The Clear method can also be used to explicitly reset Err. We can use this Err object and its methods to show user friendly error messages. Let's see the following Example. I have not used Err object here: <!-- Beginning of the ASP without Err object --> <HTML> <% ' Connect to the Database MyDSN =
"DSN=inet;UID=sa;PWD=qis" ' Open a Recordset ' Check whether required data
exists, and IF loginRs.RecordCount = 1 THEN ' Close the Recordset and the Database Connection loginRs.Close %> </HEAD> <% ' Display the Information to the Visitor if the
data was retrieved IF userId <> "" THEN Response.Write "Hi, "
& userName & "! <p>" ELSE Response.Write "Sorry, your ID is incorrect." END IF %> </BODY> <!-- End of the ASP without Err object --> To see the above code (without Err object) working, type in a Text String in the Text box below, and click on Check my credit limit here:
<!-- Beginning of the ASP with Err object --> <HTML> <% ' Instruct the Err object to
ignore errors and continue to process On Error Resume Next ' Connect to the Database MyDSN =
"DSN=inet;UID=sa;PWD=qis" ' Open a Recordset ' Check whether required data
exists, and IF loginRs.RecordCount = 1 THEN ' Close the Recordset and the Database Connection loginRs.Close %> </HEAD> <% ' Check if there was any error, IF Err.Number <> 0 THEN Response.Write "Sorry, due
to technical problems, I cannot process your request now." ELSE ' Display the Information to
the Visitor if the data was retrieved IF userId <> "" THEN
Response.Write "Hi, " & userName & "! <p>" ELSE Response.Write "Sorry, your ID is incorrect." END IF END IF %> </BODY> <!-- End of the ASP with Err object --> To see the above code (with Err object) working, type in a Text String in the Text box below, and click on Check my credit limit here: I appreciate your comments. |
||||||||||||||||||||||||||||
| Refer this site | ASPAlliance.com | Contact Us | Join | Advertise | Best Viewed with IE 4.0 or above | ||||||||||||||||||||||||||||