4

Is there a simple way to check if a connection string was successful in connecting to the specified database:

Scenario:

Connection string example:

ConnString="DRIVER={MySQL ODBC 5.1 Driver}; SERVER=###.###.####.##; PORT=3306; DATABASE=DbName; USER=DbUser; PASSWORD=DbUser_Pswrd; OPTION=3;"

If the above fails, to logon for example, as the MySQL server is currently offline or whatever is there a simple way to check that there is an issue rather than the generic error 80004005 being thrown as explained below?

Solution to this is to solve error above being thrown when a page loads where the following line(s) in the code causes error as connection is not able to connect to the database:

rs.ActiveConnection = connString

I want to catch this connection issue before the above line is read so that I can redirect to a page that has no connection string and display a message saying the server is currently offline etc.

I just need the catch error code please.

user692942
  • 16,398
  • 7
  • 76
  • 175
brabstah
  • 65
  • 1
  • 7
  • 1
    `On Error Resume Next` then after the line `If Err.Number <> 0 Then ... End If` To capture the error. – user692942 Oct 20 '15 at 14:01
  • does 'on error' catch any error event in vbscript? – brabstah Oct 20 '15 at 14:31
  • 1
    No `On Error Resume Next` doesn't catch it actually just ignores the line that caused the error but sets the `Err` objects `Number` property to the error code thrown. That then allows you to check straight after using `If Err.Number <> 0 Then`. If you don't check straight after the line you expect to error other errors could occur and not give the `Err` object the expected code. To stop ignoring errors just call `On Error Goto 0`. – user692942 Oct 20 '15 at 14:44
  • Possible duplicate of [Using Custom Error Message in Classic ASP when Database connection is down](http://stackoverflow.com/questions/19799181/using-custom-error-message-in-classic-asp-when-database-connection-is-down) – user692942 Oct 20 '15 at 14:45
  • Thanks understand, so I am assuming that on error resume is generally placed at the top of an ASP page and wherever you want to action an error event you use the code if err.number etc to do so? So in my example the if err.number code needs to be immediately after rs.activeconnection? – brabstah Oct 20 '15 at 15:23
  • 1
    Thanks for your help. Your input has helped me solve my problem. Appreciate it – brabstah Oct 20 '15 at 15:39

2 Answers2

4

VBScript Error Handling

The way to catch errors in VBScript is using the On Error Resume Next statement and Err object built into the VBScript Runtime.

What is the Err Object?

The Err object is used to contain information about errors raised at runtime, it always contains the last error raised, constantly overwriting itself as more errors are raised.

When errors are raised the execution is halted, so what is the point of Err?

This is because by default errors halt script execution. However VBScript provides a mechanism to ignore those errors and continue execution by moving to the next executable line and populate the Err object. We do this by placing a On Error Resume Next statement at the point where we want execution to ignore runtime errors.

It is important to remember that On Error Resume Next will only ignore runtime errors. Compilation Errors and Syntax Errors will still halt execution of a script.

So I'm using On Error Resume Next how do I trap an error?

With an If statement, once an error has been raised the Err object will be populated with that error and can be interrogated.

On Error Resume Next
'Line we are checking for error. When an error occurs execution will 
'move to the next line, in this case the If Err.Number <> 0 Then.
rs.ActiveConnection = connString
If Err.Number <> 0 Then
  'Raise a custom error, output error to screen or perform some action.
  'Remember to clear the Err object before continuing.
  Err.Clear
End If
'We no longer want to ignore errors
On Error Goto 0

Placing the On Error Resume Next statement resets the Err object as if the Err.Clear method had been called.

Common uses in Classic ASP

  • Sometimes you might want to detect a specific error and return a more friendly customised response, the simplest way to do this is building the response from the If Err.Number <> 0 Then statement.

    If Err.Number <> 0 Then
      Response.Write "Error (" & Err.Number & ") occurred: " & Err.Description
    End If
    
  • A more complex approach is to raise your own error, which can then be captured by others further up the execution tree (depending on the complexity of your web application).

    Err.Raise vbObjectError + 1, "My Custom App", "Connection not detected"
    
  • You might also want to capture a list of errors which can be done by using an Array or Scripting.Dictionary object instead of outputting the error immediately.

    Dim errors(), error_count
    Dim err_obj
    ...
    'This block could be repeated throughout the code for various
    'error checks.
    If Err.Number <> 0 Then
      ReDim Preserve errors(error_count)
      errors(error_count) = Array(Err.Number, Err.Description)
      error_count = error_count + 1
    End If
    ...
    'Output errors
    If IsArray(errors) Then
      Response.Write "<p>" & error_count & " error(s) have been detected</p>"
      Response.Write "<ol>"
      For Each err_obj In errors
        Response.Write "<li>Error: " & err_obj(0) & " - " & err_obj(1) & "</li>"
      Next
      Response.Write "</ol>"
    End If
    
  • Detecting various error codes that could be returned using a Select statement instead of using the classic If statement approach, like so;

    Select Case Hex(Err.Number)
    Case &H800A0006
      'Overflow error
    Case &H800A000B
      'Division by zero error
    Case Else
      'Anything else
    End Select
    

Useful Links

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
0

You would use on error resume next here, which basically tells your script to skip a line if it throws an error and continue from the next line, By checking if the err.number is not equal to 0, you can trap the error and do whatever you want from there. I would advise "disabling" on error resume next when you're done by using on error goto 0 as leaving it "enabled" throughout your code could lead to some undesirable results.

Here's a quick example of how your code would look:

err.clear
on error resume next
rs.ActiveConnection = connString
if err.number<>0 then
    '## do whatever you want here
end if
on error goto 0
user692942
  • 16,398
  • 7
  • 76
  • 175
Shaggy
  • 6,696
  • 2
  • 25
  • 45
  • 1
    Also `Err.Clear` isn't required as calling `On Error Resume Next` resets the `Err` object anyway, but you should know that already. – user692942 Oct 21 '15 at 07:22
  • 1
    It is definitely a bit rude to jump in on someone else's glory, Shaggy. – Paul Oct 21 '15 at 08:18