Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Beginner
Jul 30, 2002

Check for Errors While Connecting to a Database


When connecting to a database, you should check for errors and return a descriptive user-friendly error message. This code calls an error handler routine after connecting to the database. If an error occurs, an email is sent to inform someone (usually server admin) that an error occurred, what the error description is, and the name of the Web page. The error routine can be used as an include file.
 
<!--#INCLUDE FILE="..\..\Include\Error_Handler_Inc.asp"#-->

<%
On Error Resume Next
Set aConn = Server.CreateObject("ADODB.Connection")
aConn.Open ConnectTo()

'Error Handler Code
Dim sFileName
Dim varConnection

sFileName = Request.ServerVariables("SCRIPT_NAME")
varConnection = ConnectTo()

ErrorHandler1 Err.Number, Err.Description, varConnection, 
sFilename, ""

If err.number = 0 Then

'do your processing here
Else
	On Error GoTo 0
End If
%>

<%
Sub ErrorHandler1(varError, varDesc, varConnection, varFileName,
varOption)
	Dim varMsg
	Dim varEmailMsg

	If varError <> 0 Then
		varEmailMsg = "The following error has _
occurred: "& "<BR><I>Error Number:  _
</I>" & varError & "</I>" & _
			 "<Br><I>Error Message:  _
</I>" & varDesc & "</I>"

		varMsg = "<b><font color=red>_
An error has occurred while connecting to
the database. <p>An email has been sent to the Help Desk.</font></b>"
		Response.Write varMsg

'--------------------------------------------------
'-- Send an email to the Help Desk and Server Group
'--------------------------------------------------
		Dim strTo, strSubject, strBody
		Dim objCDOMail

		strTo = "email@address1.com; email@address2.com"
		strSubject = "Database Connection Error"
		strbody = varEmailMsg & _
"<p><I>Connection String:  </I>" _
& varConnection
& "<BR><I>File Name:  </I>" & _
varFileName & "</I>"

		Set objCDOMail = _
Server.CreateObject("CDONTS.NewMail")

		objCDOMail.From = "Name <name@email.com>"
		objCDOMail.To = strTo
		objCDOMail.Subject = strSubject
		objCDOMail.Body = strBody

		objCDOMail.BodyFormat = 0 ' CdoBodyFormatHTML
		'Outlook gives you grief unless you also set:
		objCDOMail.MailFormat = 0 ' CdoMailFormatMime

		objCDOMail.Send
		Set objCDOMail = Nothing
	End If
End Sub

%>
Jill Herrmann
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date