Debugging Active Server Pages Scripts

When you write out a script, both technical and syntactical accuracy are required. Inaccuracy may prevent a script from running properly, and may generate error messages. The process of resolving these messages is called debugging. Thoroughly testing your scripts, and debugging them if necessary, insures that all users who visit your Web site will get the intended experience rather than an error message.

Error messages are sent back as HTML with all or some the following information, depending upon the nature of the error:

·        The scripting language in which the error occurred.

·        The error number.

·        A short description of the error.

·        The name of the .asp file.

·        The line number where the error occurred.

·        The source code in which the error occurred.

·        A long description of the error and a possible fix for the error.

Note Using a text editor that displays line numbers will help you locate a line with an error in your .asp file.

Use this information to modify .asp files so that errors can be resolved. Errors of a severe nature will be sent to the Windows NT log and the Internet Information Server (IIS) log, as well as to the client browser. All other errors will be sent to the IIS log and client browser.


Error Handling with VBScript

The On Error Resume Next Statement

If an error is encountered in your .asp file, the processing of your script stops and an error message is returned to the browser. If you want to continue processing your page even if an error is encountered, include the following line at the beginning of your .asp file:

<% On Error Resume Next %>

Note The On Error Resume Next statement is a VBScript statement; it affects only scripts written in VBScript. Using this statement within an .asp file containing JScript will have no effect on JScript error debugging because JScript has no functional equivalent of resuming after an error. If you call a JScript function from VBScript and the JScript function causes an error, an error message is returned to the browser and processing of the .asp file stops at that point.

Using the On Error Resume Next statement does not actually clear an error; to manually clear the error, you can use the Err.Clear method:

            <HTML>

<HEAD>

<TITLE>Error Handling with On Error Resume Next and Err.Clear</TITLE>

</HEAD>

<BODY>

<%

Call DoSafeDivide(1, 3)

Call DoSafeDivide(1, 0)

%>

 

<SCRIPT LANGUAGE="VBScript" RUNAT=Server>

Sub DoSafeDivide(x, y)

       On Error Resume Next

       z = x / y

       If Err.Number > 0 Then

      Response.Write("Division failed:  " & x & " / " & y & "<BR>")

      Response.Write("Error source: " & Err.Source & "<BR>")

      Response.Write("Error number: " & Err.Number & "<BR>")

      Response.Write("Error description: " & Err.Description & "<BR>")

               Err.Clear

       Else

      Response.Write("Division succeeded: " & x & " / " & y & " = " & z & "<BR>")

       End If

End Sub

</SCRIPT>

</BODY>

</HTML>

The For...Each Statement

You can use the For...Each statement to iterate over a collection, thus simplifying your debugging process by returning all of the variables of a collection in a script. For example, the following script sample uses the For...Each statement with the QueryString collection of the Request object to return all values for all keys in the QueryString collection.

   = <br>

      There are  values for .<br>

        Value  is <b></b><br>

VBScript errors can generate a pointer, which indicates the exact location of the error in the script code:

Microsoft VBScript compilation error '800a03f3'

Expected '='

/ASPSamp/Samples/outstrem.asp, line 11

Set OutStream Nothing

-------------^

 

Error Handling with JScript

The following scripting mistakes commonly result in errors for JScript:

·        Misspelling of terms.

·        Incorrect capitalization (JScript is case-sensitive).

·        Unmatched pairs of brackets, parentheses, and single and double quotation marks.

Always check for these types of errors when debugging JScript.

 

Debugging Forms

If you are receiving variables in the QueryString collection of the Request object that should be in the Form collection, make sure that the HTML <FORM> tag is setting METHOD=POST. The GET method passes form variables into the QueryString collection. The POST method passes form variables into the Form collection.

Database Error Trapping Routine



<%
If err.number > 0 Then
Response.write("VBScript Errors Occured:" & "<P>")
Response.write("Error Number=" & err.number & "<P>")
Response.write("Error Descr.=" & err.description & "<P>")
Response.write("Help Context=" & err.helpcontext & "<P>")
Response.write("Help Path=" & err.helppath & "<P>")
Response.write("Native Error=" & err.nativeerror & "<P>")
Response.write("Source=" & err.source & "<P>")
Response.write("SQLState=" & err.sqlstate & "<P>")
Else
Response.write("No VBScript Errors Occured" & "<P>")
End If
If conn.errors.count> 0 then
Response.write("Database Errors Occured" & "<P>")
For counter= 0 to conn.errors.count
Response.write("Error #" & conn.errors(counter).number & "<P>")
Response.write("Error desc. -> "
& conn.errors(counter).description & "<P>")
Next
Else
Response.write "No Database Errors Occured!" & "<P>"
End If
%>


 

ADO Error Codes

Constant Name

Number

Description

AdErrInvalidArgument

3001
0x800A0BB9

The application is using arguments that are of the wrong type, are out of acceptable range, or are in conflict with one another.

AdErrNoCurrentRecord

3021
0x800A0BCD

Either BOF or EOF is True, or the current record has been deleted; the operation requested by the application requires a current record.

AdErrIllegalOperation

3219
0x800A0C93

The operation requested by the application is not allowed in this context.

AdErrInTransaction

3246
0x800A0CAE

The application may not explicitly close a Connection object while in the middle of a transaction.

adErrFeatureNotAvailable

3251
0x800A0CB3

The operation requested by the application is not supported by the provider.

AdErrItemNotFound

3265
0x800A0CC1

ADO could not find the object in the collection corresponding to the name or ordinal reference requested by the application.

AdErrObjectInCollection

3367
0x800A0D27

Can't append. Object already in collection.

adErrObjectNotSet

3420
0x800A0D5C

The object referenced by the application no longer points to a valid object.

adErrDataConversion

3421
0x800A0D5D

The application is using a value of the wrong type for the current operation.

adErrObjectClosed

3704
0x800A0E78


The operation requested by the application is not allowed if the object is closed.

adErrObjectOpen

3705
0x800A0E79

The operation requested by the application is not allowed if the object is open.

adErrProviderNotFound

3706
0x800A0E7A

ADO could not find the specified provider.

adErrBoundToCommand

3707
0x800A0E7B

The application cannot change the ActiveConnection property of a Recordset object with a Command object as its source.

adErrInvalidParamInfo

3708
0x800A0E7C

The application has improperly defined a Parameter object.

adErrInvalidConnection

3709
0x800A0E7D

The application requested an operation on an object with a reference to a closed or invalid Connection object.