473,769 Members | 6,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.execu teReader...

MyDataGrid.Data Source = rdr
MyDataGrid.Data Bind()

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 2841
Hi Joel,
MyDataGrid.Data Bind() 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********@ear thlink.net> wrote in message
news:WP******** ************@co mcast.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.execu teReader...

MyDataGrid.Data Source = rdr
MyDataGrid.Data Bind()

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********@ear thlink.net> wrote in message
news:WP******** ************@co mcast.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.execu teReader...

MyDataGrid.Data Source = rdr
MyDataGrid.Data Bind()

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;integr ated
security=SSPI;d atabase=northwi nd");

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

try
{
myConnection.Op en();
SqlDataReader myReader = mySelSqlCommand .ExecuteReader( );
Response.Write( " Data Reader is Closed ?(after
Retrieveing)" + myReader.IsClos ed.ToString());
DataGrid1.DataS ource = myReader;
DataGrid1.DataB ind();
Response.Write( "Data Reader is Closed ?(after DataBind)" +
myReader.IsClos ed.ToString());
myReader.Close( );
Response.Write( ""Data Reader is Closed ?(after Closing)" +
myReader.IsClos ed.ToString());
}
catch(SqlExcept ion se)
{
Response.Write( "Error : " + se.ToString());
}
finally
{
myConnection.Cl ose();
}

// Code in the page load -Ends

Thanks
Ashish M Bhonkiya
"Trinath" <an*******@disc ussions.microso ft.com> wrote in message
news:FC******** *************** ***********@mic rosoft.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 OracleConnectio n("connection string"
Dim lcmd As New OracleCommand(" sql query", lcon
Dim ldr As OracleDataReade

lcon.Open(
ldr = lcmd.ExecuteRea der(
DataGrid1.DataS ource = ld
DataGrid1.DataB ind(

Dim lcmd2 As New OracleCommand(" sql query", lcon
Dim ldr2 As OracleDataReade
ldr2 = lcmd2.ExecuteRe ader(
DataGrid2.DataS ource = ldr
DataGrid2.DataB ind(

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
373
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 datareader and cannot access the datareader once the connection is closed. Here is what I do: public function getDataReader() as datareader
3
1426
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 some sample code if possible. Thanks Srinivas
1
1236
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: ==================================== dgAgmtList = new DataGrid(); SqlConnection con; string sql; SqlCommand cmd; SqlDataReader reader;
4
1531
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 przyrodniczych czyni ateist±, ale na dnie
10
5340
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: protected System.Data.SqlClient.SqlConnection conn; protected System.Data.SqlClient.SqlDataReader drCountry;
5
7898
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 SqldataReader in the function where it is being created properly? If I close the SqlDataReader in the function my datareader returns nothing of course. So how do I code it properly to pass back a datareader and close it in the function?
1
1796
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 controls, and write this text to a corresponding column in the mysql database.
3
2386
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 Maritz.Learning.HP.Callcenter.BusinessServices.Searchs(base.ConnectionString); searchs.Search(this.FirstNameTextbox.Text, this.LastNameTextbox.Text,
2
2572
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 the data is not insert into the database. Please assists. Need to solve this urgently. Thanks! Public Sub InsertSvcReq(ByVal FormType As String, ByVal CustName As String, _ ByVal CustAdd As String, ByVal CustState As String, _ ByVal...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.