Solved

Correct way to Close/De-reference an object called from another class?

Posted on 2004-08-13
3
169 Views
Last Modified: 2010-04-23
I just need to know if the code below is going to successfully close/dereference/release the resources for my DataReader, Command & Connection objects.

I have 2 classes - MyUtilityClass which contains a function which will return a DataReader, and 'MyMainClass' which calls the 'GetDataReader' function in 'MyUtilityClass'.

-----------------------

(MyUtilityClass)...
 Public Function GetDataReader(ByVal intMyID As Integer) As SqlDataReader
      Dim cnnSQL As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
      Dim cmdSQL As New SqlCommand("myStoredProcedure", cnnSQL)
      Dim drdSQL As SqlDataReader

      cmdSQL.CommandType = CommandType.StoredProcedure
      cmdSQL.Parameters.Add("@MyID", SqlDbType.Int)
      cmdSQL.Parameters("@MyID").Value = intMyID

      Try
         cnnSQL.Open()
         drdSQL = cmdSQL.ExecuteReader(CommandBehavior.CloseConnection)
      Catch ex As System.Exception
         If Not drdSQL.IsClosed Then
            drdSQL.Close()
         End If
         If cnnSQL.State = ConnectionState.Open Then
            cnnSQL.Close()
         End If

         ' I throw an exception here to email admin, notifying of error details.
      End Try

      Return drdSQL
   End Function

-----------------------

(MyMainClass)
   Private Sub PopulateDropDownList()
      Dim myDropDownList As New DropDownList
      Dim myDataReader As SqlClient.SqlDataReader

      myDataReader = MyUtilityClass.GetDataReader(123)

      myDropDownList.DataSource = myDataReader
      myDropDownList.DataBind()

      myDataReader.Close()
   End Sub

-----------------------

1) The first thing I am concerned about is, I am not calling the Dispose method for either the Command or Connection objects at any point, as if I do so in a 'Finally' block within 'MyUtilityClass', the DataReader I return is going to effectively be closed/non-useable. For my other functions that are not returning a DataReader object, I like to call Dispose before returning the function value (I think this is the right thing to do?)

2) I am setting the 'CommandBehaviour' to 'CloseConnection' in the 'GetDataReader' function - Is this instance of the DataReader actually being Closed OUTSIDE of this function (i.e. in the 'PopulateDropDownList' routine in 'MyMainClass') as it is being assigned to another instance of a datareader object which is THEN being closed, or are they being treated as 2 different datareaders, 1 of which is being left open and the 2nd one which is being closed within the 'PopulateDropDownList' routine?

I'm just trying to get my head around what I should and shouldn't be doing with regards to the closing/resource releasing of these objects.

Any help much appreciated.
0
Comment
Question by:XMLnoobie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 60 total points
ID: 11798108
1) Once you've closed the object, you've released the necessary resources and the garbage collection will do the final work.  

2) There is only one datareader object in use and once you close it, the connection is closed with it.  That is one way to release the resources.  I use a standalone dataaccess class where the connection object is created in the constructor, and there's a Dispose method that releases all the resources.  That way, I can use the same dataaccess class for several calls, and then call dispose to mop up the rest.
0
 

Author Comment

by:XMLnoobie
ID: 11799069
Hi Paul, thanks for your comments - So there is nothing wrong with the way I am doing the above then?
I like the sound of how you use this standalone data access class. I don't suppose you know of any example sites that use this approach where I could reference some sample code do you?

Thanks again.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 11800234
No, I haven't seen any sample code for it--it's not a unique or new idea however.  If you go searching you might find one.  You can make your job easier by downloading and using the Microsoft Application Data Access Block, which contains generic functions for returning a dataset, datareader, executing dynamic queries, etc.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp

Then you can build your class yourself, using what you need.  
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question