Re: Errors Catching



NMAv wrote on Fri, 12 May 2006 10:20:02 -0700:

I've got a simple asp page that reads in a a string from a text box (SQL
statements) and then runs the statements. I'd like to be able to catch an
errors when they occur before they error in the asp page with such nice
errors like:

Microsoft OLE DB Provider for SQL Server error '80040e37'

Invalid object name 'test123'.

/Sillycode.asp, line 40

I tried the following but this is really error reporting and not catching.
It seems as if the error is returned from the server and appears on my asp
page before my error reporting occurs (which makes sense). What I'd like
is to be able to catch the error and return a message "nicely" on the asp
page.

'Note CmdTxt is my SQL statement string
Dim oCmd
Dim oConn
Dim oRS
Dim CriticalError
Set oConn = connectdb(SRVER,DB)
Set oCmd=server.CreateObject ("adodb.command")
Set oCmd.ActiveConnection=oConn
CmdTxt=StringReplace(CmdTxt)
oCmd.CommandText = StringReplace(CmdTxt)
oCmd.CommandType=adCmdText
oCmd.Execute

'Check for errors
If oConn.Errors.Count > 0 Then
Set Err = Server.CreateObject("ADODB.Error")
For Each Err In oConn.Errors
If Err.Number <> 0 Then
Response.Write ("Number: " & Err.Number & "<p>")
Response.Write ("Description: " & Err.Description & "<p>")
Response.Write ("Source: " & Err.Source & "<p>")
Response.Write ("SQLState: " & Err.SQLState & "<p>")
Response.Write ("NativeError: " & Err.NativeError & "<p>")
CriticalError = True
End If
Next
Set Err = Nothing
If CriticalError Then
Response.End
End If
End If

.....



Add

On Error Resume Next

above your code. This causes the raised error to be ignored by the ASP
engine, allowing your code to deal with it. Watch out for any loops in your
code getting stuck though, check for Err.Number = 0 in each cycle of a loop
to determine if you should continue. Eg.

On Error Resume Next
Set oRec = Server.CreateObject("ADODB.Recorset")
oRec.Open "asdsdsds"

Do Until oRec.EOF

blah

oRec.MoveNext

Loop

oRec.Close


The above will get stuck in the Do Until loop because oRec never has it's
EOF property set to True, as it never opens. You can fix the loop by doing this:

Do Until oRec.EOF Or Err.Number <> 0

or

Do While Not oRec.EOF And Err.Number = 0


Handling errors in ASP is a bit of a kludge, ASP.Net adds proper error
handling.

Dan


.



Relevant Pages

  • Re: ASP Loop in wrong place????
    ... below is the code for the entire table (inc. ASP functions) ... face="Verdana, Arial, Helvetica, sans-serif"> ... >>the column data and the 3rd is where the loop for the SQL statement lies. ...
    (microsoft.public.inetserver.asp.general)
  • Re: ASP Loop in wrong place????
    ... Thanks however as i will build my ASP apps dynamically in the future. ... >>about - note the extra row for the end of loop. ... > Helvetica, sans-serif";} ... > ' Get your record set before beginning to create the table ...
    (microsoft.public.inetserver.asp.general)
  • Re: ASP, looping, and stored procedures.... error 800a0bb9 ...
    ... > page PER loop. ... The looping goes by month from and to dates selected ... > In pure ASP the page takes about 15 seconds. ... > Oh, and by the way, the test box has SQL server and IIS on it. ...
    (microsoft.public.inetserver.asp.db)
  • Re: CE5 ASP - strange error message on Response.Flush
    ... For an ASP page stuck in a loop, there's no way for HTTPD to stop it because ... I guess CE HTTPD uses a different error message from IIS, ...
    (microsoft.public.windowsce.platbuilder)
  • Dont understand flow using xmlhttp
    ... I'm building a simple newsletter application using Classic ASP. ... loop through the array and for each e-mail address do a http ... Is there a way of not continuing the loop until the response has ... var aList = new Array; ...
    (comp.lang.javascript)