Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I have a database connection that I'm closing in the finally section of my error trapping. The problem is sometimes an error occurs before the connection is open so when it hits the finally the connection.close line causes another error to happen. Is there an easy way to ignore errors in the finally section of my error trapping?

 

Thanks!

  • Administrators
Posted

You would have to put a try catch block within the finally block if you want to supress errors raised there.

 

It might be easier to check for the likely causes (probably the connection object being null / nothing) and simply don't attempt to close the connection if you haven't got a valid connection.

 

Out of interest what language / version of .Net are you using? C# all versions and VB.Net from version 2.0 have a using keyword that pretty much does what you are after.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

I believe it is possible to check the state of the connection before you close that connection. The syntax would likely look like this:

 

If (yourConnection.State = ConnectionState.Open) Then
   yourConnection.Close()
End If

Amir Syafrudin

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...