473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2824
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a...
3
by: Srinivas | last post by:
Hi, My function returns a datareader object containing only one column of datatype money. In asp.net I want to bind this datareader to a dropdownlist. Can anyone suggest me how to do this with...
1
by: Mervin Williams | last post by:
I placed a datagrid on my webform, set the datasource to a datareader, and bound the reader to the datagrid. However, my datagrid does not display. Here is my code from the Page_Load method: ...
4
by: Gawel | last post by:
control.DataSource = reader; control.DataBind(); Is it possible that DataBind() will close automatically the reader ? -- Gawel ------------------------------- Pierwszy łyk z pucharu nauk...
10
by: Assimalyst | last post by:
Hi, I'm attempting to use a data reader to load data from a single table, tblCountry, with two columns, countryNo (the Key) and countryName, into a DropDownList box. Here's the code: ...
5
by: jjmraz | last post by:
Hi, I have a situation where in a dll a SqlDataReader is created on a function call but is never closed. The datareader gets passed back to the asp.net page calling it. How should I close the...
1
by: garyusenet | last post by:
Hello, I know how to create a mysql database and open it in code. I don't know how to databind to it. i have a simple form with 5 controls text controls. I want to enter text into the...
3
by: Doug Durrett | last post by:
I'm having an issue and wanted to pass it by everyone to see what you think. Here is my code. //Code Start searchs = new...
2
by: kendrick82 | last post by:
Hi, I am developing a web application and facing a difficulty in inserting the new data in a Datatable into the MS Access databse. The below method is able to execute without any error message but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.