Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I think my question is more or less about how to properly dispose of objects I've used. I'm coming over from ASP 3.0 where I've learned to dispose of everything I don't need, so I do the same thing in VB.NET, though lately as my project grows I think I'm fighting with the Garbage Collection.

I can click on a page where I access the DB and get an error like the one below (the exact error varies but most of the time it's either one of those mentioned) then I can referesh the page and everything works just fine. So it leads me to believe that the GC is kicking in at times where I'm dispousing of some objects (Readers, OleDbCommand, OleDbConnection etc.).

What's the proper way of dispousing of ojects and is that what's causing my problems ? In most VB.NET examples people don't dispouse of objects so I guess I should do the same ? Thanks for your time.

 

 

Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.

Source Error:

Line 65: MyCMD = New OleDb.OleDbCommand(SQL, MyConn)

Line 66: MyRead = MyCMD.ExecuteReader

> Line 67: MyCMD.Dispose()

Line 68: While Not MyRead.Read = False

 

 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 56: MyCMD = Nothing

Line 57: dRepeater.DataSource = DS

> Line 58: MyConn.Dispose()

Line 59: MyConn = Nothing

Line 60: dRepeater.DataBind()

  • Administrators
Posted

You can only have one active DataReader on a connection at any given time, however there is nothing preventing you using more than one DataReader as long as you close each one before using the next.

I would generally keep the connection open till the last Reader in the routine has finished and then close it, I would close each Reader as soon as I have finished using it - just to prevent having multiple active ones by accident.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

As was pointed out the your problem was not garbage collection. But I just have some notes about garbage collection that I would like to share.

 

If an object has a .Dispose method then by all means, dispose of it. It will free up any unmanaged resources. As far as garbage collection goes, I don't know if it is the VB compiler, MSIL compiler, or what exactly that does it, but something determines "safe points" where the garbage can be collected without causing you any worry or problems. Although some good practice on your part might help making memory management go more effiecently, you generally don't need to take any precautions when it comes to garbage collection .

[sIGPIC]e[/sIGPIC]
Posted

Thanks Guys. I think I'm getting the hang of it. I've managed to get rid of the first problem by removing the MyCMD.Dispouse line. Now for the 2nd problem nothing I do seems to help :(

 

There is only one active DataReader at the time, and I do close all of them. The thing is that the error can show up on any page (that accesses the DB) no matter how coded, for example this morning I go the error on this page:

 

Call OPEN_DB()
Dim MyRead As OleDb.OleDbDataReader
Dim MyCMD As New OleDb.OleDbDataAdapter("sp_hc_NEWS_VIEW", MyConn)
MyCMD.SelectCommand.CommandType = CommandType.StoredProcedure
MyCMD.SelectCommand.Parameters.Add("@ID", OleDb.OleDbType.Integer).Value = sID
MyRead = MyCMD.SelectCommand.ExecuteReader()
If Not MyRead.Read = False Then
   TITLE = MyRead.GetString(0)
   ARTICLE = MyRead.GetString(1)
   sDATE = MyRead.GetDateTime(2).ToShortDateString
   ACTIVE = CONVERT_TXT(MyRead.GetInt32(3).ToString)
   ANAME = MyRead.GetString(4)
End If
MyRead.Close()
MyCMD.SelectCommand.Connection.Close()
Page.DataBind()

 

Any ideas what else could be wrong ?

 

 

The error was:

System.NullReferenceException: Object reference not set to an instance of an object.

 

Line 43: End If

Line 44: MyRead.Close()

> Line 45: MyConn.Dispose()

Line 46: MyConn = Nothing

Line 47: Page.DataBind()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...