Solved

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

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net help 4 34
How to open form using item in Listbox. 8 23
How to share My.Settings in multiple application instances ? 7 31
Get month and date in a format 4 35
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 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