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
jamesspoAsked:
Who is Participating?
 
Jorge PaulinoIT Pro/DeveloperCommented:
Strange ...
Try to do this way instead:

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

Open in new window

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
>> 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.
0
 
jamesspoAuthor Commented:
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....
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
cquinnCommented:
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
0
 
jamesspoAuthor Commented:
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.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:

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 ?
0
 
jamesspoAuthor Commented:
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.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
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"
0
 
jamesspoAuthor Commented:
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.
0
 
jamesspoAuthor Commented:
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.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Glad I could help!
jpaulino
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.