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

  • Thread starter Patrick Olurotimi Ige
  • Start date
P

Patrick Olurotimi Ige

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;
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?
 
K

Karl Seguin

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
 
P

Patrick Olurotimi Ige

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!
 
K

Karl Seguin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top