views:

4

answers:

0

I have an SqlDataReader which I use with a data-bound control. Since I'd like to free the DB-connection as soon as it is no longer used, I'm putting the relevant code into a using-block, e.g. as suggested here:

using (SqlDataReader reader = getReader()) 
{ 
    databoundControl.DataSource = reader;
    databoundControl.DataBind(); 
} 

Is this approach safe? E.g. can I be sure that the data-bound control will not try to access its DataSource (the SqlDataReader) after the call to its DataBind() method, which would be after the reader has been disposed?

Would it be a better approach to call the reader.Close() method at a later moment (e.g. in Page_Unload when in a WebForms app)?

related questions