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. Well coded or not?

Well coded or not?

Scheduled Pinned Locked Moved Visual Basic
databasehelpquestion
3 Posts 3 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.
  • S Offline
    S Offline
    Sam Heller
    wrote on last edited by
    #1

    I have the following code. I am returning a string. If I use return where I have in this way is there a chance I could have left some database connections open still? Does it skip the connection "Finish with resources" section? I believe this is the cause to alot of timeout exceptions. Any help much appreciated. 'Gets any previosuly create about us text Public Function GetAboutUs(ByVal ProductID As Int32) As String ' Create Connection Dim HSConn As New SqlConnection(ConfigurationManager.ConnectionStrings("RemoteSqlServer").ConnectionString) ' Create data reader Dim dr As SqlDataReader 'Declare Command Dim HSComm As New SqlCommand() Try HSConn.Open() ' Provide connection HSComm.Connection = HSConn ' Provide command type and command HSComm.CommandType = CommandType.StoredProcedure HSComm.Parameters.AddWithValue("ProductID", ProductID) HSComm.CommandText = "SP_ProductAboutUsSelect" 'Execute the command dr = HSComm.ExecuteReader While dr.Read() 'If there is a Tagline return it. Otherwise return nothing If dr.IsDBNull(0) Then Return Nothing Else Return dr.GetString(0) End If End While 'Return in case value is not found Return Nothing Catch ex As Exception ' Failed to retrieve tagline so return false Emailer.ErrorEmail(ex, Page) Return Nothing End Try ' Finish up with resources dr.Close() HSConn.Close() End Function

    L P 2 Replies Last reply
    0
    • S Sam Heller

      I have the following code. I am returning a string. If I use return where I have in this way is there a chance I could have left some database connections open still? Does it skip the connection "Finish with resources" section? I believe this is the cause to alot of timeout exceptions. Any help much appreciated. 'Gets any previosuly create about us text Public Function GetAboutUs(ByVal ProductID As Int32) As String ' Create Connection Dim HSConn As New SqlConnection(ConfigurationManager.ConnectionStrings("RemoteSqlServer").ConnectionString) ' Create data reader Dim dr As SqlDataReader 'Declare Command Dim HSComm As New SqlCommand() Try HSConn.Open() ' Provide connection HSComm.Connection = HSConn ' Provide command type and command HSComm.CommandType = CommandType.StoredProcedure HSComm.Parameters.AddWithValue("ProductID", ProductID) HSComm.CommandText = "SP_ProductAboutUsSelect" 'Execute the command dr = HSComm.ExecuteReader While dr.Read() 'If there is a Tagline return it. Otherwise return nothing If dr.IsDBNull(0) Then Return Nothing Else Return dr.GetString(0) End If End While 'Return in case value is not found Return Nothing Catch ex As Exception ' Failed to retrieve tagline so return false Emailer.ErrorEmail(ex, Page) Return Nothing End Try ' Finish up with resources dr.Close() HSConn.Close() End Function

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this does not look OK; everytime you return, the remainder of the function is not executed, in particular the Close statements at the end. There are two solutions to this: 1. don't use return except as the last statement; instead store the result value in some variable (say result), and proceed throughout the function, that ends on return result 2. use a try/catch/finally construct with the Close lines in the finally block; they will get executed even when you exit the try or catch parts in whatever way (even with return). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


      1 Reply Last reply
      0
      • S Sam Heller

        I have the following code. I am returning a string. If I use return where I have in this way is there a chance I could have left some database connections open still? Does it skip the connection "Finish with resources" section? I believe this is the cause to alot of timeout exceptions. Any help much appreciated. 'Gets any previosuly create about us text Public Function GetAboutUs(ByVal ProductID As Int32) As String ' Create Connection Dim HSConn As New SqlConnection(ConfigurationManager.ConnectionStrings("RemoteSqlServer").ConnectionString) ' Create data reader Dim dr As SqlDataReader 'Declare Command Dim HSComm As New SqlCommand() Try HSConn.Open() ' Provide connection HSComm.Connection = HSConn ' Provide command type and command HSComm.CommandType = CommandType.StoredProcedure HSComm.Parameters.AddWithValue("ProductID", ProductID) HSComm.CommandText = "SP_ProductAboutUsSelect" 'Execute the command dr = HSComm.ExecuteReader While dr.Read() 'If there is a Tagline return it. Otherwise return nothing If dr.IsDBNull(0) Then Return Nothing Else Return dr.GetString(0) End If End While 'Return in case value is not found Return Nothing Catch ex As Exception ' Failed to retrieve tagline so return false Emailer.ErrorEmail(ex, Page) Return Nothing End Try ' Finish up with resources dr.Close() HSConn.Close() End Function

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #3

        No. It gave me a headache just looking at it ;P You should rewrite it a bit.

        "Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus

        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