INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Debugging ASP

How should I debug my ASP applications by BG12424
Posted: 3 Mar 03 (Edited 4 Mar 03)

The purpose of this FAQ is to help developers debug their ASP applications. No matter what your skill level is, whether a beginner or advanced ASP developer, debugging will be an essential part of developing a successful web application. Debugging, like coding, takes practice, and like coding, you will become better with it as you do it more.

In addition to my debugging techniques, I have also researched other resources, such as Tek-Tips threads and other websites to assist with the results of this tutorial, which I will reference.

The following list is a set of guidelines that should be used while developing your ASP applications.

  1. Understand what your application should accomplish. Have a proper design and know the expected results. This should minimize unexpected errors and assist your troubleshooting efforts.


  2. Understand the error messages that are displayed to you. If you are a beginner ASP developer, the error messages may not be familiar to you. Copy your error message and perform an internet search by pasting the error message in a search engine search box. Doing this will generally lead you to pertinent information that will help resolve the errors you are experiencing. If not, then the search should lead you in the right direction to investigate your errors more closely.


  3. An example of an error message (minus Line numbers) might be:

    Line 1: Error Type:
    Line 2: Microsoft JET Database Engine (0x80040E10)
    Line 3: Too few parameters. Expected 1.
    Line 4: /sandbox/PagingRS/TMPnpdob6i7g.asp, line 76


    So, with this error:
    Line 1: Gives you a descriptive header indicating that this is an error
    Line 2: Gives you the technical details of the error. Copy this line and paste it into a search engine. You'll be surprised as to what you will get in return.
    Line 3: Gives you a user friendly description of the error at hand.
    Line 4: Gives you the ASP file and Line number within that file of where the error is occurring.

  4. Include a general debugging include file at the end of your ASP page in question. This debugging include file will provide you a list of all application, session, querystrings, forms, cookies, client certificates, and server variables, which will show you the big picture.


  5. An example of this using the below debugging code is:

    << a bunch of code above >>
    </body>
    </html>
    <!--#include file="debugging.asp"-->


  6. Output variable results using the Response.write method. This will help you see the values in your ASP code at any point during the execution of your code. After you perform you response.write, do a Response.End(), which will clear your page from any further output. Also, if this is a "buggy" SQL statement then attempt to run the SQL statement locally at the database level if you can. The database application may give you a better sense as to what the problem is.


  7. An example of this is:

    strSQL = "select * from tablename where myName = " & request.form("myName")
    Response.Write strSQL
    Response.End()


    The results of this would be something like:
    select * from tablename where id = Brian Gaines

    Notice that I have not included my name in a string format using single quotes. So, now if I go back and fix my sql statement to include my name within quotes, then the sql will look like this:

    strSQL = "select * from tablename where myName = '" request.form("myName") & "'"


  8. Comment out the code that you believe to be the problem area. This will help you pinpoint which line of code is causing the error.


  9. Simplify your code. If the error is concentrated within a section of your code, remove all other code on your page that may prevent you from solving the immediate problem. By removing the clutter around the problem area, you will be allowed to focus on the problem area.


  10. In your code use the "On Error Resume Next", then after executing ADO methods, use ADOÆs Err object to get more information on the error.


  11. An example of this is (thanks to theScientist):


    On Error Resume Next

    If Err.Number > 0 Then
          Response.Write "<FONT SIZE=5><B>" & "VBScript Errors:" & "</B></FONT><P>"
          Response.Write "<B>" & "Error Number: " & "</B>" & Err.Number & "<P>"
          Response.Write "<B>" & "Error Descr: " & "</B>" & Err.Description & "<P>"
          Response.Write "<B>" & "Help Context: " & "</B>" & Err.HelpContext & "<P>"
          Response.Write "<B>" & "Help Path: " & "</B>" & Err.HelpPath & "<P>"
          Response.Write "<B>" & "Native Error: " & "</B>" & Err.NativeError & "<P>"
          Response.Write "<B>" & "Source: " & "</B>" & Err.Source & "<P>"
          Response.Write "<B>" & "SQL State: " & "</B>" & Err.SQLState & "<P>"
    End If
    IF Conn.Errors.Count > 0 Then
          Response.Write "<FONT SIZE=5><B>" & "Database Errors:" & "</B></FONT><P>"
          Response.Write "<B>" & "SQL Expression: " & "</B>" & SQL & "<P>"
      For counter= 0 To Conn.Errors.Count
            Response.Write "<B>" & "Error Number: " & "</B>" & Conn.Errors(Counter).Number & "<P>"
            Response.Write "<B>" & "Error Descr: " & "</B>" & Conn.Errors(Counter).Description & "<P>"
      Next
      Else
            << rest of code >>
    End If

  12. Review IIS's documentation by linking to it from your own local server located http://yourservername/iishelp/. IIS's documentation gives detailed information pertaining to how to build applications with ASP and IIS

  13. In addition to the above debugging techniques, research your errors on the web, review Tek-Tips FAQs, search other Tek-Tip threads (clicking the "Keyword Search" tab) for your answers.


  14. If your answer is not found anywere using all the previous steps, then post to Tek-Tips for additional guidance.



Debugging Include File (NOTE: some of the code below may wrap on your screen, so if you copy & paste it into your ASP page, then you might need to fix this):

<!-- START: DEBUGGER UTILITY -->
<style type="text/css">
.Debugger {
    font-family: Verdana,sans-serif;
    font-size: 12px;
    font-weight: normal;
}
.h2Debugger {
    font-family: Verdana,sans-serif;
    font-size: 18px;
    font-weight: bold;
}
.h3Debugger {
    font-family: Verdana,sans-serif;
    font-size: 14px;
    font-weight: bold;
}
</style>
<table border="1" bgcolor="#FFCC66" width="100%"><tr><td class="Debugger">
<center><h2 class="h2Debugger">Debugger Utility</h2></center>
<h3 class="h3Debugger"> Application Variable Collection </h3>
<%
On Error Resume Next
For Each Item in Application.Contents
    Response.Write Item & " = " & Application.Contents(Item) & "<BR>"
    For Each ItemKey in Application.Contents(Item)
        Response.Write "Sub Item: " & Item & " (" & ItemKey & ") : " & Application.Contents(Item)(ItemKey) & "<br>"
    Next
Next ' Key
%>
<h3 class="h3Debugger"> Session Variable Collection </h3>
<%
On Error Resume Next
For Each Item in Session.Contents
    Response.write Item & " = " & Session.Contents(Item) & "<BR>"
    For Each ItemKey in Session.Contents(Item)
        Response.Write "Sub Item: " & Item & " (" & ItemKey & ") : " & Session.Contents(Item)(ItemKey) & "<br>"
    Next
Next
%>

<h3 class="h3Debugger"> QueryString Collection </h3>
<%
On Error Resume Next
For Each Item in Request.QueryString
    For iCount = 1 to Request.QueryString(Item).Count
        Response.Write Item & " = " & Request.QueryString(Item)(iCount) & "<br>"
    Next
Next
%>

<h3 class="h3Debugger"> Form Collection </h3>
<%
On Error Resume Next
For Each Item in Request.Form
    For iCount = 1 to Request.Form(Item).Count
        Response.Write Item & " = " & Request.Form(Item)(iCount) & "<br>"
    Next
Next
%>

<h3 class="h3Debugger"> Cookies Collection </h3>
<%
On Error Resume Next
For Each Item in Request.Cookies
    If Request.Cookies(Item).HasKeys Then
        'use another For...Each to iterate all keys of dictionary
        For Each ItemKey in Request.Cookies(Item)
            Response.Write "Sub Item: " & Item & "(" & ItemKey & ")"
            Response.Write " = " & Request.Cookies(Item)(ItemKey)
        Next
    Else
        'Print out the cookie string as normal
        Response.Write Item & " = " & Request.Cookies(Item) & "<br>"
    End If
Next
%>

<h3 class="h3Debugger"> ClientCertificate Collection </h3>
<%
On Error Resume Next
For Each Item in Request.ClientCertificate
    For iCount = 1 to Request.ClientCertificate(Item).Count
        Response.Write Item & " = " & Request.ClientCertificate(Item)(iCount) & "<br>"
    Next
Next
%>

<h3 class="h3Debugger"> ServerVariables Collection </h3>
<%
On Error Resume Next
For Each Item in Request.ServerVariables
    For iCount = 1 to Request.ServerVariables(Item).Count
        Response.Write Item & " = " & Request.ServerVariables(Item)(iCount) & "<br>"
    Next
Next
%>
</td></tr>
</table>
<!-- END: DEBUGGER UTILITY -->


References:
Tek-Tips thread: Thread333-483346
Tek-Tips thread: Thread333-488993
http://www.4guysfromrolla.com/webtech/021099-1.shtml
http://www.4guysfromrolla.com/webtech/top10/int4.shtml

Other Resources to review:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnasp/html/asptshoot.asp
http://www.asna.com/0902_ASNA_Visions_tip.asp

If there are comments you would like to make or you would like to contribute to this FAQ, please, feel free to send a message through this FAQ. Also, please take a moment to rate this FAQ, so changes can be made if necessary and we can all improve our debugging techniques.

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close