#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0

    Proper db connection error handling ?


    Hi all,
    Usualy when there is a problem connecting your db,
    the vb script stops working and just displays the error...

    How to handle a connection problem properly (without having the vb script stop working??)
    I did:

    ...
    On Error Resume Next
    objDB.Open strConn
    On Error GoTo 0

    'connect ok ?
    '-------------
    If objDB.State = adStateOpen Then
    WScript.Echo("connect ok")

    'do somthing in db
    ...
    objDB.Close

    Else
    WScript.Echo("connect failed")
    WScript.Echo(Err.Description)

    End If
    ...

    but I just cant get the proper Error.Description :-(
    How to ?

    Thanks for your help!
    acki4711
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    45
    With either "On Error Resume Next" or "On Error GoTo 0", you have told the routine to ignore errors. You need to trap the errors to handle them with something like "On Error GoTo ErrorHandler".

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,440
    Rep Power
    4539
    VBScript doesn't have on error goto <handler>, you have to make your own error checking test after each statement you want to verify. Easiest to help track your database problem is just to comment out the on error resume next statement and re-run your code. You should get any error messages caused by your script.
    ======
    Doug G
    ======
    Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
    --Alfred Bester
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0

    Try this:-


    try
    {
    SqlCeConnection conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
    new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
    // do some stuff
    }

    conn.Close();
    }
    catch (Exception ex)
    {
    ExceptionManager.HandleException(ex);
    }

    Surely a better way would be to declare a connection object before the try, establish a connection inside the try block and close it in a finally block?

    SqlCeConnection conn = null;
    try
    {
    conn = new SqlCeConnection(ConnectionString);

    conn.Open();

    using (SqlCeCommand cmd =
    new SqlCeCommand("SELECT stuff FROM SomeTable", conn))
    {
    // do some stuff
    }
    }
    catch (Exception ex)
    {
    ExceptionManager.HandleException(ex);
    }
    finally
    {
    if( conn != null ) conn.Close();
    }

IMN logo majestic logo threadwatch logo seochat tools logo