IIS 7 and Above
Deliberate DSN-Less Connection Failure to SQL Server Database
Last post Nov 08, 2018 11:01 PM by Johnny Steel
Nov 07, 2018 01:29 AM|Johnny Steel|LINK
I'm getting an (expected) error message with a DSN-Less connection to a SQL database on a SQL Server that says:
Microsoft OLE DB Provider for SQL Server
[DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied.
Now, i know why the connection failed as it was deliberate on my part to
cause this error. (Normally, the DSN-Less connection works very well indeed.) I want to trap this error and convert it to text to say the database is not available, such as when it is shut down to do software maintenance on it, etc., rather
than display the error message like the one given above.
How do i trap this error and convert it into some meaningful text on the web page to say the database is not available?
The code i am using is Classic ASP.
Nov 08, 2018 06:51 AM|Brando Zhang|LINK
Hi Johnny Steel,
As far as I know, you could catch the error where you think it's going to occur using on error resume next.
on error resume next.
then check for the error code in the following line of code.
Alternately you could scan the server logs for 500 errors. or set up a "500 error" page in your IIS settings.
On Error Resume Next
... do something...
If Err.Number <> 0 Then
... handle error
Nov 08, 2018 11:01 PM|Johnny Steel|LINK
thank you, Brando, your suggestion worked. Never thought of that. I'm not used to handling errors in my code.
What I finished doing was:
ON ERROR RESUME NEXT
conntemp.open myDSN 'This activates the database connection, as given above this code.
IF ERR.NUMBER <>0 THEN
Then in the web page, retrieved the value for DBConnection and responded accordingly.
Problem sorted. Thank you.