Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8600561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:38:41+00:00 2026-06-12T01:38:41+00:00

I ran across this bit of code (not mine) and it weirded me out

  • 0

I ran across this bit of code (not mine) and it weirded me out a little bit.

Public Overridable Function Save(ByVal UpdateUserID As Integer) As Integer
    Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("mcle").ToString())

    Try
        conn.Open()
        Return Save(conn, UpdateUserID)
    Finally
        conn.Close()
    End Try
End Function

Public Overridable Function Save(ByVal conn As SqlConnection, ByVal UpdateUserID As Integer) As Integer
    If Me.activityID <> 0 Then
        Return SaveAct(conn, UpdateUserID)
    Else
        Return AddAct(conn, UpdateUserID)
    End If
End Function

(For reference, SaveAct and AddAct are both long functions that add a bunch of parameters and update the database)

Now, is it kosher to pass the open connection as a parameter or could this lead to problems? Not breaking so far, just wondering what the best practice is here.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:38:42+00:00Added an answer on June 12, 2026 at 1:38 am

    Passing an open connection is perfectly okay to do… though normally I’d invert that and use a function that returns an open and ready connection into the existing function. So it might look something like this:

    Public Overridable Function Save(ByVal UpdateUserID As Integer) As Integer
        Using cn As SqlConnection = GetConnection()
            If Me.activityID <> 0 Then
                Return SaveAct(conn, UpdateUserID)
            Else
                Return AddAct(conn, UpdateUserID)
            End If
        End Using
    End Function
    

    This code would be part of a data access layer, such that only certain methods/classes can see the GetConnection() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran across this bit of code and to me it seems like its
I ran across this chunk of code (modified) in our application, and am confused
I recently ran across this puzzle, was finally able to struggle out a hacky
I'm making this little program because I was bored and I ran across this
I ran across this pattern in the code of a library I'm using. It
I ran across this code in one of our Entity Framework applications. I know
Ran across this recently and wondering if someone out there can give me a
I ran across this segment of code (in .m implementation file in an IBAction
I recently ran across this code in one of the projects I'm working on,
While reading Programming Ruby , I ran across this code snippet: while gets num1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.