Dispose

M

Morten Snedker

Will this reader object ever be disposed properly?

Function MerchandiseStatus() As IDataReader

sb.Append(...)

Dim reader As IDataReader = Database.getDataReader(sb.ToString)
Return reader
reader.Close()
reader.Dispose()

End Function


If not, how is the proper way to have a function return an IDataReader
that is disposed after use? Should I build a class instead with a
dispose method?


/Regards
 
M

Mark Rae [MVP]

Morten Snedker said:
Will this reader object ever be disposed properly?

Function MerchandiseStatus() As IDataReader

sb.Append(...)

Dim reader As IDataReader = Database.getDataReader(sb.ToString)
Return reader
reader.Close()
reader.Dispose()

End Function


If not, how is the proper way to have a function return an IDataReader
that is disposed after use? Should I build a class instead with a dispose
method?


Presumably, Database.getDataReader is your DAL?

If so, make sure you are calling ExecuteReader on your SqlCommand object
with the CommandBehavior.CloseConnection option set:
http://msdn2.microsoft.com/en-us/library/aa326246(VS.71).aspx

Also, consider the Using word which will automatically dispose any objects
which implement the IDisposable interface when it's done with them:
http://msdn2.microsoft.com/en-us/library/htd05whh.aspx

However, you might like also to consider this:
http://www.dotnetfun.com/articles/ado.net/DoNotUseCommandBehaviorCloseConnectionWithUsing.aspx
 
P

Peter Bromberg [C# MVP]

As Mark alluded to, you cannot close a DataReader before you use it since a
live DataReader holds open a connection. So the idea of even thinking about
calling the Close or Dispose method on a datareader when your method's return
value is of type IDataReader is moot.

I generally avoid this type of programming pattern as it is prone to develop
problems when others use your code. In other words, do the business logic of
whatever MerchandiseStatus is inside your method, close the reader (with
CommandBehavior.CloseConnection) and THEN return your "merchandiseStatus -
whatever it may be.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
 
M

Milosz Skalecki [MCAD]

Howdy,

In addition to the input from the guys, please note return statement exits
from the function returning the result therefore statement (BTW compiler
should warn you about that situation):
Return reader
' below lines will not be executed
reader.Close()
reader.Dispose()

Return it is not equivalent to just assigning the result (as classic vba
statement is) it leaves the function immidiatley returning the result,
 
S

Scott M.

To just say, in a bit more simpler terms, what the others have said....

It's the caller's job to worry about disposing the objects it calls.

Think about it, Microsoft doesn't put code into the connection class that
disposes of it, that's your job as the user of the connection class. All
Microsoft (or anyone building a class) can do is provide the caller with a
mechanism for Disposal, but that caller will have to know enough to use it.

-Scott
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top