views:

329

answers:

3

When you assign an active IDataReader object to either a Repeater, GridView, etc., does it automatically get closed upon completion of the DataBind method call or do we still need to explicitly close it ourselves?

this.sampleRepeater.DataSource = ExampleDAL.GetIDataReader();
this.sampleRepeater.DataBind();
A: 

I believe you have to close it yourself. The Repeater's DataBind, for instance, does not close its data source. If it is not an IDataSource, then it simply casts it as IEnumerable and calls foreach.

Ruslan
A: 

I checked the code for Repeater using Reflector and it does not close the IDataReader. You'll have to do it yourself.

Jakob Christensen
+2  A: 

With the DataReader type, the connection needs to remain open while you're accessing the data. This is not a disconnected data source like say, a DataTable. You must close this out yourself else you're waiting for it to die of natural causes:).

Interested in alternatives? Put your data in to a DataTable, DataSet, or convert the returned rows in to a List of custom objects and return one of those.

Hope that helps!

Ian Suttle
I'll second using a DataTable instead.
ProfK
Thanks. I know about the benefits of datatables and such. I am looking through a client's code base and am seeing quite a few samples of my above code. I just wanted a second opinion before I started making fixes.
Dan Appleyard