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%>

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS