First and foremost I'd wonder why you're going ToString() when ConnectionStings["name"].ConnectionString (in C# at least) is actually what you're looking for ...
Don't get overly granular with try/catch -- wrap your entire operation, not just the retrieval of the connection string. Also, try using an AppSettingsReader or a custom static Settings class to present this sort of stuff to the rest of the app. You shouldn't be writing ConfigurationManager.AppSettings or .ConnectionStrings["some string value that you're likely to typo, if not have a hard time remembering, period"] all over the place. Finally, use something like the Enterprise Library Exception Handling Application Block (EHAB) and Logging Application Block (LAB) to give you configuration-driven control over your exception handling. Using "exception policies" you can configure the app to log and throw the exception during development, then flip the switch to log and sink the exception in production. (Peter, they may be bloated, but they get the job done. Suggest alternatives if you will.) ∞ Andy Badera ∞ +1 518-641-1280 ∞ This email is: [ ] bloggable [x] ask first [ ] private ∞ Google me: http://www.google.com/search?q=andrew%20badera On Mon, Oct 12, 2009 at 10:07 AM, wallj <[email protected]> wrote: > > 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 >
