|
Database Error Trapping: Opening
Database
by Charles Carroll
Many people want to intercept specific errors and replace
them with friendly messages. Here is some sample code that catches mispelled DSNs and bad
login/user ids:
filename=/learn/test/dbtroubleshootopen.asp
<html><head>
<title>dbtroubleshoot.asp</title></head>
<body>
<%
on error resume next
Set Conn = Server.CreateObject("ADODB.Connection")
my_DSN="DSN=student;uid=student;password=magic2"
conn.open my_DSN
Call CheckOpen
Conn.Close
set conn=nothing
SUB CheckOpen
If err.number>0 then%>
<b>VBScript Errors Occured:</b><br>
Error Number=<%=err.number%><br>
Error Descr.=<%=err.description%><br>
Help Context=<%=err.helpcontext%><br>
Help Path=<%=err.helppath%><br>
Native Error=<%=err.nativeerror%><br>
Source=<%=err.source%><br>
SQLState=<%=err.sqlstate%><P>
<%else%>
No VBScript problems occured!<p>
<%end if
IF conn.errors.count> 0 then%>
<b>ADO Reports these Database Error(s):</b><br>
<%
maxerrors=conn.errors.count-1
for counter= 0 to maxerrors
DBErrorNum=conn.errors(counter).number
DBErrorDesc=conn.errors(counter).description
SELECT CASE DBErrorNum
CASE -2147467259
response.write "Problem => <b>Bad DSN</b><br>"
response.write "DSN => <b>" & my_DSN & "</b><br>"
CASE -2147217843
response.write "Problem => <b>Bad Login Info</b><br>"
response.write "DSN => <b>" & my_DSN & "</b><br>"
exit sub
CASE ELSE%>
Error # = <b><%=DBErrorNum%></b><br>
Error description = <b><%=DBerrorDesc%></b><p>
<%END SELECT
next
else%>
Everything Went Fine
<%
end if
END SUB%>
</body></html>
|