468,315 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

Does DataBind close a datareader?

I am attempting to verify whether a datareader is closed by calling the
databind on a control.

for example (fake code)
Dim rdr as SqlDataReader
rdr = mycommand.executeReader...

MyDataGrid.DataSource = rdr
MyDataGrid.DataBind()

Is the DataReader closed at this point or do I still need to do rdr.Close()?

Best I can tell, it is closed but I'm not quite sure how to verify it.
Nov 18 '05 #1
5 2543
Hi Joel,
MyDataGrid.DataBind() does not close your datareader, to test this.

try to retrieve the FieldCount property of the datareader after calling the
databind() method
now call the close method of datareader and then try to retrieve the
fieldcount property an exception will be thrown.

HTH
Regards
Ashish M Bhonkiya
"Joel Reinford" <jr********@earthlink.net> wrote in message
news:WP********************@comcast.com...
I am attempting to verify whether a datareader is closed by calling the
databind on a control.

for example (fake code)
Dim rdr as SqlDataReader
rdr = mycommand.executeReader...

MyDataGrid.DataSource = rdr
MyDataGrid.DataBind()

Is the DataReader closed at this point or do I still need to do rdr.Close()?
Best I can tell, it is closed but I'm not quite sure how to verify it.

Nov 18 '05 #2
Hi Joel,

DataBind does close the datareader.

You can verify this:
Datareader uses connected model (i.e.) as long as the datareader is open you can't use the same connection object for other purposes.
This will result in an Exception.
But after doing a DataBind (without closing the datareader) if you try to use the connection object , it doesn't raise any exception.
Well this means that the data reader is closed and connection is free to be used for other things.
Nov 18 '05 #3
It should still be open.

To check test rdr.IsClosed

Hope this helps.
"Joel Reinford" <jr********@earthlink.net> wrote in message
news:WP********************@comcast.com...
I am attempting to verify whether a datareader is closed by calling the
databind on a control.

for example (fake code)
Dim rdr as SqlDataReader
rdr = mycommand.executeReader...

MyDataGrid.DataSource = rdr
MyDataGrid.DataBind()

Is the DataReader closed at this point or do I still need to do rdr.Close()?
Best I can tell, it is closed but I'm not quite sure how to verify it.

Nov 18 '05 #4
Hi Trinath,

Can you check datareader's IsClosed property and confirm if the reader
object gets closed after the databind method on the datagrid.

I have tried this leme know if i am doing something wrong here.

// Code in the page load
SqlConnection myConnection = new
SqlConnection("server=ISC1XR\\EINSTIEN;integrated
security=SSPI;database=northwind");

SqlCommand mySelSqlCommand = new SqlCommand("Select * from
Customers", myConnection);

try
{
myConnection.Open();
SqlDataReader myReader = mySelSqlCommand.ExecuteReader();
Response.Write(" Data Reader is Closed ?(after
Retrieveing)" + myReader.IsClosed.ToString());
DataGrid1.DataSource = myReader;
DataGrid1.DataBind();
Response.Write("Data Reader is Closed ?(after DataBind)" +
myReader.IsClosed.ToString());
myReader.Close();
Response.Write(""Data Reader is Closed ?(after Closing)" +
myReader.IsClosed.ToString());
}
catch(SqlException se)
{
Response.Write("Error : " + se.ToString());
}
finally
{
myConnection.Close();
}

// Code in the page load -Ends

Thanks
Ashish M Bhonkiya
"Trinath" <an*******@discussions.microsoft.com> wrote in message
news:FC**********************************@microsof t.com...
Hi Joel,

DataBind does close the datareader.

You can verify this:
Datareader uses connected model (i.e.) as long as the datareader is open you can't use the same connection object for other purposes. This will result in an Exception.
But after doing a DataBind (without closing the datareader) if you try to use the connection object , it doesn't raise any exception. Well this means that the data reader is closed and connection is free to

be used for other things.
Nov 18 '05 #5
Hi Ashish

Here is what the MSDN documentation says
"Note that while a DataReader is open, the Connection is in use exclusively by that DataReader. You will not be able to execute any commands for the Connection, including creating another DataReader, until the original DataReader is closed.

So I tried this

Dim lcon As New OracleConnection("connection string"
Dim lcmd As New OracleCommand("sql query", lcon
Dim ldr As OracleDataReade

lcon.Open(
ldr = lcmd.ExecuteReader(
DataGrid1.DataSource = ld
DataGrid1.DataBind(

Dim lcmd2 As New OracleCommand("sql query", lcon
Dim ldr2 As OracleDataReade
ldr2 = lcmd2.ExecuteReader(
DataGrid2.DataSource = ldr
DataGrid2.DataBind(

Both the datagrids did get displayed ie. page ran without exceptions
If the MSDN documentation is correct then I should get an error, becos I am executin
another command without closing the first data reader. So I thought that DataBind doe
close the DataReader

I have executed your example and saw that the data reader is not closed after DataBind. Hmm..

Well I just dont know what to say
Any comments

Rgds
Trinath.
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by sam | last post: by
3 posts views Thread by Srinivas | last post: by
1 post views Thread by Mervin Williams | last post: by
4 posts views Thread by Gawel | last post: by
5 posts views Thread by jjmraz | last post: by
3 posts views Thread by Doug Durrett | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.