ASP Debugging Guideline

ASP Debugging Guideline

By: Adrian Lanning

Original Edition: 05 / 04 / 03

Last Changed: 08 / 05 / 03



Ok, you’ve created this nifty ASP page but when you try to view it, something doesn’t work. What are you going to do?

Here’s some suggestions of where to start from somebody who’s spent quite a few lifetimes debugging his own code…

(PS. These suggestions assume VBScript as the scripting language in use but they should be applicable to JavaScript as well.)

Specific Areas to Check

  1. Make sure all variables are defined somewhere before you use it.
  2. Make sure your connection and recordset objects are created before you try to open/use them.
  3. Make sure you are Dim-ing the variable only once (be especially careful of this with Rs and Conn objects. Once you Dim them once in the file, you can’t Dim them again even if you
    Set Conn = Nothing
    . You will have to
    Set Conn = Server.CreateObject(“…”)
    again tho.)
  4. Make sure your Connection String is pointing to the correct database.
  5. Make sure your SQL Query is correct!!!
    To test this, replace the sql statement with:
    sqlstr = “SELECT * FROM Sheet1”
    Response.Write (“Success!”)
    after you open the record set.
    If you see Success! on the page then you know the problem is with your SQL Query.
  6. Be careful when using Response.Redirect. Make sure that you aren’t outputting any text/html (even "<html>...") to the web page before you use it or it won’t work.

General Debugging Methods

  1. Place Response.Write (“Made it here…”) kind of statements on the page to find out where exactly the error is.
    If nothing displays on the page, the error is probably on the previous page so put the Response.Write statements there…
  2. Try to use variables as much as possible so you don’t have to type stuff in more than once. You can even use
    <!--#INCLUDE FILE=”file.asp”-->
    and Dim and set the variables in file.asp.

More Tips from Readers

Zack Halbrecht has some great suggestions for improving ASP debugging! Here are his tips:

  1. Add Option Explicit to the top of your ASP Code. Be it in a header file or just at the top of a page, it will make debugging much much easier. It amazes me how many new ASP developers don't know about this. It enforces the good practice of making sure your variables are dimmed and complains if you mistakenly dim a variable more than once.
  2. If you're using classic ASP (not .NET) and using IE as your testing/debugging tool, make sure you have the
    'Show Friendly HTTP Error Messages'
    option unchecked in IE
    Tools -> Internet Options -> Advanced Tab

    That way IE won't hide the specifics of what the 500 error is. It will actually give you a reason, file, and line number to inspect.
  3. Sometimes it will appear that this error message is not showing up, but often times this is caused by the error occurring in a section of the page that has not had a chance to render. (Code execution was broken by error before valid HTML was written to the client)
    To view the error, view the source of the page and scroll to the bottom of the source code to identify what went wrong.