Solved

How to catch a failed database connection

Posted on 2008-10-13
11
781 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now