473,385 Members | 1,622 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 5029
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.