Scope of Opening a SQLConnection from a compiled class

Discussion in 'ASP .Net Web Controls' started by Mark A. Deal, Jan 19, 2005.

  1. Mark A. Deal

    Mark A. Deal Guest

    I have a compiled class that contains two functions. One opens a database
    connection and another closes the connection.

    If I have a button on a form that calls OpenDatabase(ErrorMessage) and then
    call CloseDatabase from the same class, am I operating on the same
    connection?

    '***************************************************************************
    *

    ' Opens connection to the database *

    '***************************************************************************
    *

    Public Function OpenDatabase(ByRef DBError As String) As Boolean

    SW_Connection = New SqlClient.SqlConnection(ReadRegKey("ConnectionString"))

    Try

    SW_Connection.Open()

    Catch ex As Exception

    DBError = "An error occurred opening connection to database: " & ex.Message

    Return (False)

    End Try

    Return (True)

    End Function

    '***************************************************************************
    *

    ' Closes connection to the database *

    '***************************************************************************
    *

    Public Function CloseDatabase() As Boolean

    SW_Connection.Close()

    End Function


    --
    Mark A. Deal
    Document & Data Solutions, LLC
    http://www.docsol.com
    Time Matters AIC
    HotDocs Certified Consultant
    GhostFill Certified Consultant
     
    Mark A. Deal, Jan 19, 2005
    #1
    1. Advertising

  2. You created a heap object and saved the reference to the object in
    SW_Connection. If you didn't change that reference somewhere in your code,
    then the SW_Connection.Close() call,
    closes the previously opened connection. If you changed the reference
    somewhere in your code, you aren't closing the connection that you first
    opened. This is a resource leak.

    --
    Regards,
    Alvin Bruney

    Shameless Author plug
    The Microsoft Office Web Components Black Book with .NET
    http://tinyurl.com/27cok

    "Mark A. Deal" <> wrote in message
    news:OAKOU5b$...
    >I have a compiled class that contains two functions. One opens a database
    > connection and another closes the connection.
    >
    > If I have a button on a form that calls OpenDatabase(ErrorMessage) and
    > then
    > call CloseDatabase from the same class, am I operating on the same
    > connection?
    >
    > '***************************************************************************
    > *
    >
    > ' Opens connection to the database *
    >
    > '***************************************************************************
    > *
    >
    > Public Function OpenDatabase(ByRef DBError As String) As Boolean
    >
    > SW_Connection = New
    > SqlClient.SqlConnection(ReadRegKey("ConnectionString"))
    >
    > Try
    >
    > SW_Connection.Open()
    >
    > Catch ex As Exception
    >
    > DBError = "An error occurred opening connection to database: " &
    > ex.Message
    >
    > Return (False)
    >
    > End Try
    >
    > Return (True)
    >
    > End Function
    >
    > '***************************************************************************
    > *
    >
    > ' Closes connection to the database *
    >
    > '***************************************************************************
    > *
    >
    > Public Function CloseDatabase() As Boolean
    >
    > SW_Connection.Close()
    >
    > End Function
    >
    >
    > --
    > Mark A. Deal
    > Document & Data Solutions, LLC
    > http://www.docsol.com
    > Time Matters AIC
    > HotDocs Certified Consultant
    > GhostFill Certified Consultant
    >
    >
     
    Alvin Bruney [MVP], Jan 19, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    1,011
    Paul Opal
    Oct 11, 2004
  2. Meidan
    Replies:
    14
    Views:
    620
    Meidan
    Dec 15, 2005
  3. Klaus Schneider
    Replies:
    1
    Views:
    584
    Rolf Magnus
    Dec 2, 2004
  4. Steven T. Hatton
    Replies:
    9
    Views:
    523
  5. lander
    Replies:
    5
    Views:
    627
    bruce barker
    Mar 5, 2008
Loading...

Share This Page