Solved

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

Posted on 2004-08-13
3
156 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now