Home All Groups Group Topic Archive Search About

sqlconnection error trapping

Author
15 Nov 2005 5:35 PM
Mike
I've been trying to find examples where, if I made a typo in the "my
connectionstring", how can I gracefully exit the application.  Otherwise the
user crashes hard.  Say if the Server is down and it tries to make a
connection, what kind of code should I be writting to say the Database
doesn't exist.

VB.NET:
Imports System.Data.SqlClient
Dim oSQLConn As SqlConnection = New SqlConnection()
oSQLConn.ConnectionString="my connectionstring"
oSQLConn.Open()

Author
16 Nov 2005 12:06 AM
S.M. Altaf [MVP]
Place your code in a Try...Catch block.  It'll look something like

Try
'Your code
Catch sqlx As SqlException
MessageBox.Show(sqlx.Message)
End Try

This is just an example though, you can do whatever you like with the
exception.

HTH
Altaf
--------------------------------------------------------------------------------
All that glitters has a high refractive index.
www.mendhak.com


Show quote
"Mike" <mdunlap@nospam.com> wrote in message
news:%23$fuHsg6FHA.3588@TK2MSFTNGP15.phx.gbl...
> I've been trying to find examples where, if I made a typo in the "my
> connectionstring", how can I gracefully exit the application.  Otherwise
> the user crashes hard.  Say if the Server is down and it tries to make a
> connection, what kind of code should I be writting to say the Database
> doesn't exist.
>
> VB.NET:
> Imports System.Data.SqlClient
> Dim oSQLConn As SqlConnection = New SqlConnection()
> oSQLConn.ConnectionString="my connectionstring"
> oSQLConn.Open()
>
Author
16 Nov 2005 3:02 PM
Patrice
My personal preference would be anyway to have a last safety net so that you
at least :
#1 Write down some details for later diagnostic
#2 Have a gracefull generic message for the user

Then and only if you think it is something that your application could do
something about, you can wrap the appropriate portion inside a try catch
block so that your application can "self-correct" the problem...

It depends what kind of app. A windows app provides
AppDomain.UnhandledException and Application.UnhandledException events. An
ASP.NET provides also an event in the global.asax. If you want to catch a
particular exception this is done through a try/catch block
--
Patrice

Show quote
"Mike" <mdunlap@nospam.com> a écrit dans le message de
news:%23$fuHsg6FHA.3588@TK2MSFTNGP15.phx.gbl...
> I've been trying to find examples where, if I made a typo in the "my
> connectionstring", how can I gracefully exit the application.  Otherwise
the
> user crashes hard.  Say if the Server is down and it tries to make a
> connection, what kind of code should I be writting to say the Database
> doesn't exist.
>
> VB.NET:
> Imports System.Data.SqlClient
> Dim oSQLConn As SqlConnection = New SqlConnection()
> oSQLConn.ConnectionString="my connectionstring"
> oSQLConn.Open()
>
>

AddThis Social Bookmark Button