Re: Errors Catching
- From: "Daniel Crichton" <msnews@xxxxxxxxxxxxxxxx>
- Date: Wed, 17 May 2006 09:02:34 +0100
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
.
- Prev by Date: Re: Error registering when installing MDAC
- Next by Date: Re: adOpenForwardOnly
- Previous by thread: Re: Error registering when installing MDAC
- Next by thread: Re: Record Count from Data Reader
- Index(es):
Relevant Pages
|
|