How to catch a failed database connection
Posted on 2008-10-13
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.
Dim testDBConnConnection = New SqlConnection("Server=" & dbServerName & ";initial catalog=" & dbName & ";uid=" & dbUser & ";pwd=" & dbPass & ";Connection Timeout=5;")
Catch extestDBConn As Exception
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?