Hi Guys
This is a simple one, just more of a discussion really. But I'd like
some opinions as trying to improve my exception handling skills.
The following Sub is using ADO.NET to insert into a table via stored
procedure. My question is regarding the inclusion of
'ConfigurationManager.ConnectionStrings("myDB").ToString' within the
try block. The connection string should always be available in
web.config, its something in my control and therefore shouldn't
require any error handling. However if it isn't there its because
something exceptional has happened. So do I put within try..catch???
I could check that its there first, and handle the error without
exceptions if its not, but i don't want to add the overhead on this
one. I also understand that creating try.. catch adds hardly any
overhead unless an exception occurrs. What do people think about
this ?? Thank you in advance, JBo
Public Sub InsertPerson(ByVal _firstName As String, ByVal _lastName As
String)
Dim sqlParameter As SqlParameter
Try
sqlConn = New SqlConnection
(ConfigurationManager.ConnectionStrings("myDB").ToString)
sqlConn.Open()
sqlCommand = New SqlCommand("spName", sqlConn)
sqlCommand.CommandType =
CommandType.StoredProcedure
sqlParameter = New SqlParameter(INPARAM_FIRSTNAME,
SqlDbType.NVarChar)
sqlParameter.Value = _firstName
sqlCommand.Parameters.Add(_sqlParameter)
sqlParameter = New SqlParameter(INPARAM_LASTNAME,
SqlDbType.NVarChar)
sqlParameter.Value = _lastName
sqlCommand.Parameters.Add(_sqlParameter)
sqlCommand.ExecuteNonQuery()
Catch ce As ConfigurationErrorsException
'TODO: exception handling
Catch se As SqlException
'TODO: exception handling - here the sql server returned
an error
Catch ex As Exception
Finally
If sqlConn.State = ConnectionState.Open Then sqlConn.Close
()
End Try
End Sub