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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling & Subs

Status
Not open for further replies.

superman74

Technical User
Mar 13, 2002
6
US
I'm new at attempting Error handling, as well as subs. I'm doing the best I can in following along with ASP Bible.

Here's what I have for the ERROR HANDLER:

<%
'LEADS DATABASE CONNECTION ==============================
Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Open &quot;dsn&quot;

'ERROR HANDLING =========================================
Sub HandleError(strLocation)

if Err.Number = 0 then Exit Sub
%>
<table>
<tr>
<td><h3>Page Error</h3></td>
</tr>
<tr>
<td width=&quot;17%&quot;><b>Error #:</b></td>
<td width=&quot;83%&quot;><%=Err.Number%></td>
</tr>
<tr>
<td width=&quot;17%&quot;><b>Description:</b></td>
<td width=&quot;83%&quot;><%=Err.Description%></td>
</tr>
<tr>
<td width=&quot;17%&quot;><b>Source:</b></td>
<td width=&quot;83%&quot;><%=Err.Source%></td>
</tr>
<tr>
<td width=&quot;17%&quot;><b>URL:</b></td>
<td width=&quot;83%&quot;><%=Request.ServerVariables(&quot;URL&quot;)%></td>
</tr>
<tr>
<td width=&quot;17%&quot;><b>Location:</b></td>
<td width=&quot;83%&quot;><%=strLocation%></td>
</tr>
</table>
</body>
</html>
<%
Response.End
End Sub
%>

I'm somewhat confused with how to actually run it on the page I'm testing. I have the above handler added in an include. I thought I was suppose to add &quot;HandleError(strLocation&quot; where ever I wanted to check for an error, like below (I'm sure someone will scold me for being sloppy - I'm learning):

<%
On Error Resume Next

'RETRIEVE LOGIN ID ======================================
IDSql=&quot;SELECT TOP 1 * FROM tblLogins WHERE Name = &quot; & sName
Set rsID=Conn.Execute(IDSql)

'ERROR SUB
HandleError(&quot;Retrieve Login ID&quot;)

do while not rsID.eof
strLoginID = rsID(&quot;LoginID&quot;)
rsID.movenext
loop
rsID.close

'GET TODAYS DATE
datDate = Now()
strDate = FormatDateTime(datDate, 2)


'CREATE RECORDSET =======================================
Set RS = Server.CreateObject( &quot;ADODB.Recordset&quot; )
RS.ActiveConnection = Conn
RS.CursorType = adOpenStatic
RS.LockType = adLockPessimistic

'Add New Entry To Database
RS.Open &quot;SELECT * FROM tblLogin_Date WHERE DateID = ''&quot;
RS.AddNew

'ERROR SUB
HandleError(&quot;Add Login Date entry&quot;)

RS( &quot;LoginDate&quot; ) = strDate
RS( &quot;LoginID&quot; ) = strLoginID
RS( &quot;EmailFrom&quot; ) = strFromDate
RS.Update
RS.Close
%>

I also have the HandleError() a few more places. When I purposely create an error by misspelling a table names somewhere, I get some very strange results. It displays the &quot;Page Error&quot; the next line &quot;Error#&quot;, then drops down, moves over and does it again, over and over. I'm sure I'm doing something wrong, which is why I'm here.

Superman74 - knucklehead!!!
 
well i send you correct direction..

The way you are coding-trapping error is not the right one..

Code:
<%
If Err.number<>0 then
				'Clear  response buffer
				Response.Clear

					'Display Database Error Message to user
					    If IsObject(objConn)then
					           If objConn.Errors.Count>0 then%>
					               <b>Database Connection Object</b>
					                <%For intLoop=0 to objConn.Errors.Count -1%>
										Error No:<%=objConn.Errors(intLoop).Number %><br>
										Description:<%=objConn.Errors(intLoop).Description %><br>
										Source:<%=objConn.Errors(intLoop).Source%><br>
										SQLState:<%=objConn.Errors(intLoop).SQLState%><br>
										NativeError:<%=objConn.Errors(intLoop).NativeError%><p>
					                <%Next
					           End if
					   End if
					  
					  'Display General Error Message to user
					   If Err.Number <>0 then %>
					       <b>Page Error Object</b><br>
					         Error Number <%=Err.Number%><br>
					         Error Description<%=Err.Description%><br>
					         Source<%=Err.Source%><br>
					         Line Number <%=Err.Line%><p> 
					     <%
					   End If
					 End if
			
%>

is the basic error handler.Keep it in include file.And include it on every page you want to trap errrors.
Now put
Code:
<%on error resume next%>
on the top of the page.
Now instantiate ADO object with the same name(objConn)as above file.

Now your code is ready.Whenever error will encounter,
it will be trapped in error object and then close your ADO as well Recordset object.
Code:
<% 
'Closing the recordset as well as the connection
Call CloseRecordset() 	
Call CloseConnection()   
%>

If not understtod mail me i will send you code. Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
Rushi Shroff, I could use some samples, as I'm trying to implement some error handling for my email page (see my recent message - Another Problem with CDONTS and a loop.

I understand how to create the error handler, but not how to implement it in different areas on the page to be checked for errors.
 
Rushi, yes please! Sample code would be great. You can post it here for Rexolio's benefit too or you can send it to me at superman@actionfreight.com!

Thanks!
 
Sorry Rexolio,
As code is too long i can not paste it here.
Send me your email i will send you the code.. Rushi Shroff Rushi@emqube.com
&quot;Life is beautiful.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top