Does DataBind close a datareader?

J

Joel Reinford

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.
 
A

Ashish M Bhonkiya

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
 
G

Guest

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.
 
A

Ashish M Bhonkiya

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 said:
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.
 
G

Guest

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.
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top