Link to home
Start Free TrialLog in
Avatar of jamesspo
jamesspo

asked on

How to catch a failed database connection

Hi Experts, I have a windows form (VS 2008) that connects to a database via a connection string (shown below):

If any of the connection details are wrong the form just hangs. I have tries using a try.. catch.. finally block, but this does not seem to prevent the hanging condition.

        Try
            Dim testDBConnConnection = New SqlConnection("Server=" & dbServerName & ";initial catalog=" & dbName & ";uid=" & dbUser & ";pwd=" & dbPass & ";Connection Timeout=5;")
            testDBConnConnection.Open()
            testDBConnConnection.Close()
        Catch extestDBConn As Exception
            MsgBox("Error")
        End Try
        MsgBox("Test complete")

The problem is that the hanging connection does not get trapped by the try...chatch..finally block. I have tried "catch ex as sqlexception" as well.

All I want to do is catch any connection errors before the user starts to use the form. How do I catch incorrect connection strings, without the form hanging?

Many thanks

James
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

>> All I want to do is catch any connection errors before the user starts to use the form. How do I catch incorrect connection strings, without the form hanging?

Why don't you start a new thread in the form load and detect if the connection is ok ? That way the form doesn't hang.
Avatar of jamesspo
jamesspo

ASKER

The problems is that when the form opens I must do the DB check straight away, because the user can immediately start to interact with the database.

I'd like to be able to check the connection and when if it is found to be OK, then make the form controls active, otherwise direct the user to corectly complete the database connection dialog....
The connection object's timeout may be set to 0 (no timeout) - try setting it to a small number (seconds) - this may bring it back and allow the try/catch to work
I have set the connection timeout as follows:

Connection Timeout=5; (I have tried one second and that does not work either)

But the form still hanges for many minutes.... no catch.

BTW thanks for the quick responses! Much appreciated.

The problem is just the first time, because then the connection is quick. Why dont you do a splash screen to test the connection before open the form ?
I really need to be able to confirm that the connection is valid, without a long timeout. The application is not suited to a splash screen and it would habve to be open for a loooong time!

 I'm not sure why the try...catch...finally solution in my opening post does not work -- I'd have to be 100% sure that the database is connected before I let the user start to use the form.
But the sqlexception should be fine for you. Check this example from MSDN
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(VS.80).aspx
"This class is created whenever the .NET Framework Data Provider for SQL Server encounters an error generated from the server"
Still no joy. The program just hangs on the open connection (command.Connection.Open()). I've copied MSDN exactly and just added my own connection string (shown in my initial post)

I've set command timeout to 1 second - and there is still an indefinite hang, when I deliberately set the IP address of the server incorrectly (a possible user error).

Any othe rideas.
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jpaulino, Thank you for your time and effort. I took you advice and also lowered the timeout. I'm not 100% sure that I am doing this the best way, but your help and solution works perfectly, and I am gratefully accepting it. Many thanks.
Glad I could help!
jpaulino