ASPAlliance Home
Advertise with us
About Prasad KVNM
Refer this Site
                                               
Home Handling Database Errors KV's Kool ASP
Search
Samples/Articles
Convert URLs into Hyperlinks

Advertisement Management Systems

AdRotator Component

Extending AdRotator Component

Simple Alternative to AdRotator

A more powerful Ad Management System

Opinion Poll

Suggestions/Snippets
Passing Special Characters through URL

Date delimiter for Access and SQL Server

CDBL instead of CSNG

Handling Database Errors

Avoid Partial Updates to the Database

Book Review
C# and the .NET Platform

Favorite Links
ASPLists.com
ASPNG.com
Computer Dictionary
LearnASP.com

Microsoft .NET
Search @ Google
Wrox Press
Freecode.com

Site by Prasad KVNM,
New Jersey, USA
prasad@kunisetty.com
last updated on
22nd Oct, 2001



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.
Please try after some time.

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>
<HEAD>
<TITLE>KV's Kool ASP - Handling Errors</TITLE>

<%

    ' Connect to the Database

    MyDSN = "DSN=inet;UID=sa;PWD=qis"
    Set MyConn = Server.CreateObject("ADODB.Connection")
    MyConn.Open MyDSN

    ' Open a Recordset

    Set loginRs = Server.CreateObject("ADODB.Recordset")
    sqlStr = "SELECT userId, userName, creditLimit FROM users " & _
        "WHERE userId = '" & Request.Form(userId) & "'"
    loginRs.Open sqlStr, MyConn, 3

    ' Check whether required data exists, and
    ' if exists, read the data into Variables

    IF loginRs.RecordCount = 1 THEN
        userId = loginRs("userId")
        userName = loginRs("userName")
        creditLimit = loginRs("creditLimit")
    ELSE
        userId = ""
    END IF

    ' Close the Recordset and the Database Connection

    loginRs.Close
    MyConn.Close

%>

</HEAD>
<BODY>
<FONT face="Arial, Helvetica, sans-serif" 
size=2>

<%

' Display the Information to the Visitor if the data was retrieved
' else show a message.

IF userId <> "" THEN

    Response.Write "Hi, " & userName & "! <p>"
    Response.Write "Your Credit Limit is: " & creditLimit

ELSE

    Response.Write "Sorry, your ID is incorrect."

END IF

%>

</BODY>
</HTML>

<!-- 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:

  

   


Now, let's use the Err object in the above example:

<!-- Beginning of the ASP with Err object -->

<HTML>
<HEAD>
<TITLE>KV's Kool ASP - Handling Errors</TITLE>

<%

    ' Instruct the Err object to ignore errors and continue to process
    ' and reset the Err object

    On Error Resume Next

    ' Connect to the Database

    MyDSN = "DSN=inet;UID=sa;PWD=qis"
    Set MyConn = Server.CreateObject("ADODB.Connection")
    MyConn.Open MyDSN

    ' Open a Recordset

    Set loginRs = Server.CreateObject("ADODB.Recordset")
    sqlStr = "SELECT userId, userName, creditLimit FROM users " & _
        "WHERE userId = '" & Request.Form(userId) & "'"
    loginRs.Open sqlStr, MyConn, 3

    ' Check whether required data exists, and
    ' if exists, read the data into Variables

    IF loginRs.RecordCount = 1 THEN
        userId = loginRs("userId")
        userName = loginRs("userName")
        creditLimit = loginRs("creditLimit")
    ELSE
        userId = ""
    END IF

    ' Close the Recordset and the Database Connection

    loginRs.Close
    MyConn.Close

%>

</HEAD>
<BODY>
<FONT face="Arial, Helvetica, sans-serif" 
size=2>

<%

' Check if there was any error,
' if so, display a friendly error message.
' else display the information they requested.

IF Err.Number <> 0 THEN

    Response.Write "Sorry, due to technical problems, I cannot process your request now."
    Response.Write "<br>Please try after some time."

ELSE

    ' Display the Information to the Visitor if the data was retrieved
    ' else show a message.

    IF userId <> "" THEN

         Response.Write "Hi, " & userName & "! <p>"
        Response.Write "Your Credit Limit is: " & creditLimit

    ELSE

         Response.Write "Sorry, your ID is incorrect."

    END IF

END IF

%>

</BODY>
</HTML>

<!-- 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