Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How should I debug my ASP applications

Debugging ASP

How should I debug my ASP applications

by  BG12424  Posted    (Edited  )
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.
[ol]
[li]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.[/li]

[li]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.[/li]

An example of an error message (minus Line numbers) might be:
Code:
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:
Code:
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.
[li]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.[/li]

An example of this using the below debugging code is:
Code:
<< a bunch of code above >>
</body>
</html>
<!--#include file="debugging.asp"-->

[li]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.[/li]

An example of this is:
Code:
strSQL = "select * from tablename where myName = " & request.form("myName")
Response.Write strSQL
Response.End()

The results of this would be something like:
Code:
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:
Code:
strSQL = "select * from tablename where myName = '" request.form("myName") & "'"

[li]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.[/li]

[li]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.[/li]

[li]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.[/li]

An example of this is (thanks to theScientist):

Code:
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
[li]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
[/li]
[li]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.[/li]

[li]If your answer is not found anywere using all the previous steps, then post to Tek-Tips for additional guidance.[/li]
[/ol]

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):
Code:
<!-- 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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top