sqlconnection error trapping

M

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()
 
S

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
 
P

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top