Solved

How to catch a failed database connection

Posted on 2008-10-13
11
787 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:jamesspo
  • 5
  • 5
11 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22703241
>> 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
 

Author Comment

by:jamesspo
ID: 22703338
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
 
LVL 15

Expert Comment

by:cquinn
ID: 22703400
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:jamesspo
ID: 22703510
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22703546

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
 

Author Comment

by:jamesspo
ID: 22703794
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22703863
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
 

Author Comment

by:jamesspo
ID: 22704910
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
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 22704990
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
 

Author Closing Comment

by:jamesspo
ID: 31406288
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
 
LVL 48

Expert Comment

by:jpaulino
ID: 22848680
Glad I could help!
jpaulino
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question