473,385 Members | 2,003 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,385 software developers and data experts.

Invalid attempt to FieldCount when reader is closed error

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()
chart.DataXValueField = "Product"
chart.DataYValueField = "Price"
chart.DataSource = reader
chart.DataBind()
reader.Close()
ChartControl1.Charts.Add(chart)

Dim chart1 As New LineChart()
chart1.DataSource = reader
chart1.DataXValueField = "Product2"
chart1.DataYValueField = "Price2"
Getting the error here---> chart1.DataBind()
ChartControl1.Charts.Add(chart1)

reader.Close()
ChartControl1.RedrawChart()

With the function:-
Function GetReader() As IDataReader

Dim connection As new OleDbConnection("")

Dim command As new OleDbCommand("SELECT Top 10 [ProductName] As
Product,[UnitPrice] As Price, [UnitsInStock] As Stock FROM Products
order by UnitPrice desc", connection)
connection.Open()

Return command.ExecuteReader(CommandBehavior.CloseConnect ion)

End Function
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #1
2 5030
Patrick:
Return command.ExecuteReader(CommandBehavior.CloseConnect ion)

closes the connection after ExecuteReader() is called and thus closing the
datareader (the datareader is tied to the connection). You must either keep
the connection open and close it and the reader when you are done with it,
or make use of a dataset.

Personally I'd recommend you use the DataSet because then you end up with a
situation where 1 function (GetReader()) creates your connnection and opens
it, whereas calling function is responsible for cleaning up that
resource...definetly messy....Another solution would be to pass the
connection in as a parameter to GetReader() but I'd still favour DataSets
over this...

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/index.aspx - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Patrick Olurotimi Ige" <ig*@iprimus.com.au> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
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()
chart.DataXValueField = "Product"
chart.DataYValueField = "Price"
chart.DataSource = reader
chart.DataBind()
reader.Close()
ChartControl1.Charts.Add(chart)

Dim chart1 As New LineChart()
chart1.DataSource = reader
chart1.DataXValueField = "Product2"
chart1.DataYValueField = "Price2"
Getting the error here---> chart1.DataBind()
ChartControl1.Charts.Add(chart1)

reader.Close()
ChartControl1.RedrawChart()

With the function:-
Function GetReader() As IDataReader

Dim connection As new OleDbConnection("")

Dim command As new OleDbCommand("SELECT Top 10 [ProductName] As
Product,[UnitPrice] As Price, [UnitsInStock] As Stock FROM Products
order by UnitPrice desc", connection)
connection.Open()

Return command.ExecuteReader(CommandBehavior.CloseConnect ion)

End Function
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 19 '05 #2
Thanks Karl for the response.
I did a research too and now i know the limitations of DataReader!
I'll implement using DataSet..
Thanks
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #3

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

Similar topics

2
by: Brent Burkart | last post by:
Below is the error I am receiving. I have checked SQL Profiler and it is receiving the correct query which runs fine in Query Analyzer. Any ideas? Server Error in '/lockinsheet' Application....
0
by: CW | last post by:
I get this FieldCount error when I attempt to bind a datagrid with a dataset, not a datareader object. The code snippet is as belows: 'PopulateForm called in Page Load event Private Sub...
3
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 =...
4
by: Dave | last post by:
I'm using a datareader to get data from an sql table. The line that gives the error is as follow, dtrReceivers.ToString() which gives the error, Invalid attempt to read when no data is...
1
by: sivam.solai | last post by:
Public Function GetTopLevelThreadName(ByVal ThreadID As Integer) As SqlDataReader Sp_Datasql = "WS_Get_TopLevelThreadName" SqlParam = New SqlParameter(0) {} Try SqlParam(0) = New...
4
by: MarkusR | last post by:
If I run the stored proc in the Query Analyzer this works and I get the expected result set back. However when I run it in my application I get a results set of one row but when I try to access the...
0
by: mwmorrison93 | last post by:
Hello, I'm at my wit's end regarding the "The state information is invalid for this page and might be corrupted" error and I'm hoping someone can provide some assistance. I working with C#/ASP.NET...
2
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...
1
by: mark.norgate | last post by:
Hi I have a problem with a repeater and a data connection. In my Page_Load, I have the following code: Dim connectionString As String = WebConfig.ConnectionString Dim connection As...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.