Check for Errors While Connecting to a Database

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.

 <%On Error Resume NextSet aConn = Server.CreateObject("ADODB.Connection")aConn.Open ConnectTo()'Error Handler CodeDim sFileNameDim varConnectionsFileName = Request.ServerVariables("SCRIPT_NAME")varConnection = ConnectTo()ErrorHandler1 Err.Number, Err.Description, varConnection, sFilename, ""If err.number = 0 Then'do your processing hereElse	On Error GoTo 0End If%><%Sub ErrorHandler1(varError, varDesc, varConnection, varFileName,varOption)	Dim varMsg	Dim varEmailMsg	If varError <> 0 Then		varEmailMsg = "The following error has _occurred: "& "
Error Number: _" & varError & "" & _ "
Error Message: _" & varDesc & "" varMsg = "_An error has occurred while connecting tothe database.

An email has been sent to the Help Desk." Response.Write varMsg'--------------------------------------------------'-- Send an email to the Help Desk and Server Group'-------------------------------------------------- Dim strTo, strSubject, strBody Dim objCDOMail strTo = "[email protected]; [email protected]" strSubject = "Database Connection Error" strbody = varEmailMsg & _"

Connection String: " _& varConnection& "
File Name: " & _varFileName & "" Set objCDOMail = _Server.CreateObject("CDONTS.NewMail") objCDOMail.From = "Name " 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 IfEnd Sub%>

See also  Digital Signage Solutions for Agile Software Development Teams

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist