Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to catch a failed database connection

Posted on 2008-10-13
11
788 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 1 28
VBA - Excel, Hide/unhide range of rows on sheet with listbox selection 9 63
Generate Numbers in JQuery file 11 66
Groupbox Control ? 2 17
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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