Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. silly exception question

silly exception question

Scheduled Pinned Locked Moved Visual Basic
questiondatabaseannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kowplunk
    wrote on last edited by
    #1

    I have various classes in my data access layer that are talking with the database. for all the methods I pass in the connection ref. Now if the connection object is being passed in from the outside and within the da classes there is a try catch block is it necessary to enclose the methods call within a try catch finally block to close the connection in the finally? You would think that any exceptions that are thrown will be caught internally and there for the connection will still be able to be close even if it is not in a finally block in the outer function. private sub formMethod() ... conn.open 'not sure if this has to be in the t/c block someDAMethod() conn.close 'connection should still be closed, right? end sub ..in da module public sub someDAMethod() try ... catch sqlEx as sqlException end try end sub I guess you could say that you are only catching sqlExceptions so you still have to do the other catch, but you could always catch the base exception, as long as you know that the update didn't go through properly. I am still new to dealing with exceptions properly, so if it is a dumass question sorry. I am just curious.

    D 1 Reply Last reply
    0
    • K kowplunk

      I have various classes in my data access layer that are talking with the database. for all the methods I pass in the connection ref. Now if the connection object is being passed in from the outside and within the da classes there is a try catch block is it necessary to enclose the methods call within a try catch finally block to close the connection in the finally? You would think that any exceptions that are thrown will be caught internally and there for the connection will still be able to be close even if it is not in a finally block in the outer function. private sub formMethod() ... conn.open 'not sure if this has to be in the t/c block someDAMethod() conn.close 'connection should still be closed, right? end sub ..in da module public sub someDAMethod() try ... catch sqlEx as sqlException end try end sub I guess you could say that you are only catching sqlExceptions so you still have to do the other catch, but you could always catch the base exception, as long as you know that the update didn't go through properly. I am still new to dealing with exceptions properly, so if it is a dumass question sorry. I am just curious.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      kowplunk wrote: conn.open 'not sure if this has to be in the t/c block someDAMethod() conn.close 'connection should still be closed, right? Actually, your connection code should be in your someDAmethod code, not on your form. This way, your connection is handled where it belongs, in your Data Access method. In there, your connection can be closed in the Try/Catch/Finally block (inside Finally). It would go something like this:

      Try
      ' setup our database connection.
      Dim conn As SqlConnection = GetConnection()
      Dim comm As New SqlCommand("blaa blaa blaa")
      Dim param As New SqlParameter(blaa blaa blaa)
      .
      .
      (more code to setup and execute our data access)
      Catch ex As Exception
      ' Code to log the exception, throw a custom exception, or whatever to handle a failed request.
      Finally
      ' Check to see if the connection is anything but closed! Don't check for Open!
      If conn.State <> ConnectionState.Closed Then
      conn.Close()
      End If
      comm = Nothing
      conn.Dispose()
      End Try

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        kowplunk wrote: conn.open 'not sure if this has to be in the t/c block someDAMethod() conn.close 'connection should still be closed, right? Actually, your connection code should be in your someDAmethod code, not on your form. This way, your connection is handled where it belongs, in your Data Access method. In there, your connection can be closed in the Try/Catch/Finally block (inside Finally). It would go something like this:

        Try
        ' setup our database connection.
        Dim conn As SqlConnection = GetConnection()
        Dim comm As New SqlCommand("blaa blaa blaa")
        Dim param As New SqlParameter(blaa blaa blaa)
        .
        .
        (more code to setup and execute our data access)
        Catch ex As Exception
        ' Code to log the exception, throw a custom exception, or whatever to handle a failed request.
        Finally
        ' Check to see if the connection is anything but closed! Don't check for Open!
        If conn.State <> ConnectionState.Closed Then
        conn.Close()
        End If
        comm = Nothing
        conn.Dispose()
        End Try

        RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        K Offline
        K Offline
        kowplunk
        wrote on last edited by
        #3

        ok, I figured that sometime there was a few different sprocs that were run, that passing in the connection would prevent the opening and closing of each one, but I see how keeping everything together is a good thing as well. Thanks for the tips

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups