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

Home Posts Topics Members FAQ

Closed DataReader

Hi,

I have a class A with a public method GetReader:
public OleDbDataReader GetReader()
{
SqlConnection myConnection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["ConnStr"]);
SqlCommand myCommand = new SqlCommand("CMR C_Top10List", myConnection);

myCommand.Comma ndType = CommandType.Sto redProcedure;

myConnection.Op en();
SqlDataReader result =
myCommand.Execu teReader(Comman dBehavior.Close Connection);
return result;
}
Class A is used as following:

DataGrid TopTen;
A top10 = new A();

SqlDataReader result = null;

try
{
result = top10.GetReader ();
TopTen.DataSour ce = result;
TopTen.DataBind ();
result.Close();
}
catch(Exception ex)
{
if (!result.IsClos ed)
{
result.Close();
}
return;
}

The grid is not bind to the data in database, and I receive the following
error:
“Invalid attempt to FieldCount when reader is closed."

Why is reader closed after the return from the method GetReader? I would
expect the reader being open till I explicitly close him.

Thanks for a help.

Lubomir

Nov 19 '05 #1
3 1867
The reader is closed because you are closing the underlying connection....a
reader always connected to the database and thus requires an open
connection. The connection is closing because you've specified the
CloseConnection behavior....

You could remove the commandBehavior ,but then when/how will you close the
connection? Your two options are

(a) use a dataset instead which is disconnected form the database (Doesn't
require an open connection after the data is fetched)
(b) Pass the connection into the method, so that your calling code has a
reference to it which IT can be responsible for closing..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Lubomir" <Lu*****@discus sions.microsoft .com> wrote in message
news:0C******** *************** ***********@mic rosoft.com...
Hi,

I have a class A with a public method GetReader:
public OleDbDataReader GetReader()
{
SqlConnection myConnection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["ConnStr"]);
SqlCommand myCommand = new SqlCommand("CMR C_Top10List", myConnection);

myCommand.Comma ndType = CommandType.Sto redProcedure;

myConnection.Op en();
SqlDataReader result =
myCommand.Execu teReader(Comman dBehavior.Close Connection);
return result;
}
Class A is used as following:

DataGrid TopTen;
A top10 = new A();

SqlDataReader result = null;

try
{
result = top10.GetReader ();
TopTen.DataSour ce = result;
TopTen.DataBind ();
result.Close();
}
catch(Exception ex)
{
if (!result.IsClos ed)
{
result.Close();
}
return;
}

The grid is not bind to the data in database, and I receive the following
error:
"Invalid attempt to FieldCount when reader is closed."

Why is reader closed after the return from the method GetReader? I would
expect the reader being open till I explicitly close him.

Thanks for a help.

Lubomir

Nov 19 '05 #2
Hello Karl,

CommandBehavior .CloseConnectio n closes the connection when the DataReader
is closed. Are you suggesting that the because the connection goes out of
scope, it is closed? I would think that if a reference was still being used
it wouldnt be gc'd. My GC knowledge is pretty limited though.

The thing that I find interesting is that OleDbDataReader is the type returned
from the GetReader method, however the variable result (which is what's returned)
is a type of SqlDataReader.

I'm not sure that this could should even compile, but if it does, I would
think this has something to do with it.

Of course, this whole thing could be cited as a reason for not returning
SqlDataReader from your methods (which FxCop flags as an error).

--
Matt Berther
http://www.mattberther.com
The reader is closed because you are closing the underlying
connection....a reader always connected to the database and thus
requires an open connection. The connection is closing because you've
specified the CloseConnection behavior....

You could remove the commandBehavior ,but then when/how will you close
the connection? Your two options are

(a) use a dataset instead which is disconnected form the database
(Doesn't
require an open connection after the data is fetched)
(b) Pass the connection into the method, so that your calling code has
a
reference to it which IT can be responsible for closing..
Karl

"Lubomir" <Lu*****@discus sions.microsoft .com> wrote in message
news:0C******** *************** ***********@mic rosoft.com...
Hi,

I have a class A with a public method GetReader:
public OleDbDataReader GetReader()
{
SqlConnection myConnection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["ConnStr"]);
SqlCommand myCommand = new SqlCommand("CMR C_Top10List",
myConnection);
myCommand.Comma ndType = CommandType.Sto redProcedure;

myConnection.Op en();
SqlDataReader result =
myCommand.Execu teReader(Comman dBehavior.Close Connection);
return result;
}
Class A is used as following:

DataGrid TopTen;
A top10 = new A();
SqlDataReader result = null;

try
{
result = top10.GetReader ();
TopTen.DataSour ce = result;
TopTen.DataBind ();
result.Close();
}
catch(Exception ex)
{
if (!result.IsClos ed)
{
result.Close();
}
return;
}
The grid is not bind to the data in database, and I receive the
following
error:
"Invalid attempt to FieldCount when reader is closed."
Why is reader closed after the return from the method GetReader? I
would expect the reader being open till I explicitly close him.

Thanks for a help.

Lubomir


Nov 19 '05 #3
Well, you learn something every day...that's certainly something I've gotten
wrong in the past. Thaxn for clearing it up....It certainly makes more
sense (and has more value) this way....

Now that you point that out, I'd guess the same thing you did..the
inconsistancy between OleDb and Sql...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Matt Berther" <mb******@hotma il.com> wrote in message
news:22******** *************** ****@news.micro soft.com...
Hello Karl,

CommandBehavior .CloseConnectio n closes the connection when the DataReader
is closed. Are you suggesting that the because the connection goes out of
scope, it is closed? I would think that if a reference was still being
used it wouldnt be gc'd. My GC knowledge is pretty limited though.

The thing that I find interesting is that OleDbDataReader is the type
returned from the GetReader method, however the variable result (which is
what's returned) is a type of SqlDataReader.

I'm not sure that this could should even compile, but if it does, I would
think this has something to do with it.

Of course, this whole thing could be cited as a reason for not returning
SqlDataReader from your methods (which FxCop flags as an error).

--
Matt Berther
http://www.mattberther.com
The reader is closed because you are closing the underlying
connection....a reader always connected to the database and thus
requires an open connection. The connection is closing because you've
specified the CloseConnection behavior....

You could remove the commandBehavior ,but then when/how will you close
the connection? Your two options are

(a) use a dataset instead which is disconnected form the database
(Doesn't
require an open connection after the data is fetched)
(b) Pass the connection into the method, so that your calling code has
a
reference to it which IT can be responsible for closing..
Karl

"Lubomir" <Lu*****@discus sions.microsoft .com> wrote in message
news:0C******** *************** ***********@mic rosoft.com...
Hi,

I have a class A with a public method GetReader:
public OleDbDataReader GetReader()
{
SqlConnection myConnection = new
SqlConnection(C onfigurationSet tings.AppSettin gs["ConnStr"]);
SqlCommand myCommand = new SqlCommand("CMR C_Top10List",
myConnection);
myCommand.Comma ndType = CommandType.Sto redProcedure;

myConnection.Op en();
SqlDataReader result =
myCommand.Execu teReader(Comman dBehavior.Close Connection);
return result;
}
Class A is used as following:

DataGrid TopTen;
A top10 = new A();
SqlDataReader result = null;

try
{
result = top10.GetReader ();
TopTen.DataSour ce = result;
TopTen.DataBind ();
result.Close();
}
catch(Exception ex)
{
if (!result.IsClos ed)
{
result.Close();
}
return;
}
The grid is not bind to the data in database, and I receive the
following
error:
"Invalid attempt to FieldCount when reader is closed."
Why is reader closed after the return from the method GetReader? I
would expect the reader being open till I explicitly close him.

Thanks for a help.

Lubomir


Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3806
by: jazzandlatin | last post by:
Hi everybody Could somebody tell me how it is possible to check that any connection opened in a page be closed when the page has been published ? My problem is that SQL server looks saturated very quickly. I 've checked the code but something must be wrong.. there are lots of sleeping connections in SQLServer... might this be the reason and how can i solve the problem Thanks a lot Jazz
10
1688
by: Brent | last post by:
I'm quickly running out of available db connections with the code below. I like to think I'm closing everything as I go along, but I watch the connections shoot up as soon as the the method getIDs comes into play. Does anyone see anything obvious? It's almost as if my code can't close a DB connection as fast as it can open it... Thanks for any help! --Brent
2
5063
by: Patrick Olurotimi Ige | last post by:
Why do i get "Invalid attempt to FieldCount when reader is closed" Is the problem the way the datareader reads data as opposed to a dataset? When trying to compile this code:- Dim reader As IDataReader = GetReader() Dim chart As New LineChart()
3
3037
by: Patrick Olurotimi Ige | last post by:
With the code below i get error:- Invalid attempt to read data when reader is closed. //Get a datareader SqlDataReader objDataReader; objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection); datalistoutput.DataSource = objDataReader;
15
8286
by: Rob Nicholson | last post by:
I'm starting to worry a bit now. We're getting the above error when two users hit the same database/page on an ASP.NET application using ADO.NET, talking to a SQL 7 server. The error is perfectly repeatable :-( But this should help! The error is occurring inside ExecuteReader which uses a DataReader internally. Here are some things that I'm pretty sure it's *NOT*:
10
6110
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the code below which when executed generates the following error message: 'There is already an open datareader with this command which must be closed first' Private Sub MainMenu_Load(ByVal sender As System.Object, ByVal e As
13
3415
by: Bart | last post by:
Hi, i get the error: "There is already an open DataReader associated with this Command which must be closed first" Thanks Bart ----------------------------------------- Imports System.Data.sqlclient
3
13196
by: BLUE | last post by:
I've a TransactionScope in which I select some data and I want to do some queries for each record retrieved with my select. I'm using a DataReader and for each record I do factory.CreateCommand() and then I execute the command, but I get the following exception message: " There is already an open DataReader associated with this Command which must be closed first. " Searching on google I've found I've to create another connection, but...
2
6942
by: Chris | last post by:
Hi, i wrote this code for fetching data from a table using a stored procedure. The connection definition and opening are put in a class. The actual read of data occurs in code-behind. My problem with this code: Invalid attempt to Read when reader is closed at line: While dtreader.Read() Thanks for help
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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...
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...
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?
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.