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

DataReader - Invalid attempt to read data when reader is closed.

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.CloseConn ection);

datalistoutput.DataSource = objDataReader;
datalistoutput.DataBind();

if (objDataReader["Sub_Category_Text"].ToString() == "asdas")
{
datalistoutput.Visible = true;
}
else
{
datalistoutput.Visible = false;
}

And if i want to check if "Sub_Category_Text" is NULL how can i do it
using my objDataReader?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #1
3 3017
You can't...when you bind a DataReader to the DataSource of a control, you
read through the entire datareader and thus can't go back. You need to
either return it as a 2nd result and use objDataReader.nextResult or use a
dataset...

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!)
"Patrick Olurotimi Ige" <ig*@iprimus.com.au> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
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.CloseConn ection);

datalistoutput.DataSource = objDataReader;
datalistoutput.DataBind();

if (objDataReader["Sub_Category_Text"].ToString() == "asdas")
{
datalistoutput.Visible = true;
}
else
{
datalistoutput.Visible = false;
}

And if i want to check if "Sub_Category_Text" is NULL how can i do it
using my objDataReader?

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

Nov 19 '05 #2
Thx Karl i ended up not using DataBind which worked as u adviced!
Is DataReader really faster then DataSet?
Cos i can't really see much difference btw them!


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #3
It's situational. If you have many rows or many users than you'll start to
see a difference. The difference between using a DataSet or DataReader for
100 rows probably can't be measured by human perception. But make it 1000
rows with 100 users at the same time and you might start to feel the
difference :)

On the flip side, you can cache the DataSet...which will likely make it
faster (situational again), but will use up some memory (that's what memory
is there for, so do it, just don't eat up too much!)

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!)
"Patrick Olurotimi Ige" <ig*@iprimus.com.au> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Thx Karl i ended up not using DataBind which worked as u adviced!
Is DataReader really faster then DataSet?
Cos i can't really see much difference btw them!


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

Nov 19 '05 #4

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

Similar topics

6
by: Yasutaka Ito | last post by:
Hi, My friend had a little confusion about the working of DataReader after reading an article from MSDN. Following is a message from him... <!-- Message starts --> I was going thru DataReader...
2
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
3
by: Lubomir | last post by:
Hi, I have a class A with a public method GetReader: public OleDbDataReader GetReader() { SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings); SqlCommand...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
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...
10
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...
4
by: rikleo2001 | last post by:
Hello Experts. I need your help, I am new to XML web service, and I have to complete one task. Here is current situation. 1. I have web service running to recieve SOAP message from an...
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...
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...
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...
0
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...

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.