Invalid attempt to read when no data is present

G

Guest

I'm using a datareader to get data from an sql table. The line that gives the
error is as follow,

dtrReceivers["Site_V"].ToString()

which gives the error,

Invalid attempt to read when no data is present

which is correct. The line works when data is in the row. But shouldn't the
data reader return a null! How can I check to see if data is in the row?

Thanks in advance,

David
 
C

Christopher Reed

No, because it has no row to acquire a null value from. This is why
dtrReceivers.Read() returns a false when it is out of rows to read.
 
C

Chris Dunaway

I get this error when I try to get the value of a column without first
calling the .Read method of the DataReader. Generally, you should
check the return from the .Read method:

While dtrReceivers.Read
'Read the values here
End While

..Read will return false when there are no more rows to read.
 
G

Guest

Thanks for the replys.

You are right there was no row being returned. There was a bug in my program
that I found that cause a invalid row to be selected. I thought that it was
giving the
error only when a valid row was being selected and reading the empty field,
since
it worked with one selection and not another.

Chris answer solved my problem, thanks.
 
C

Chris Dunaway

What you can do is check the DataReader's HasRows property. Then you
can handle the problem gracefully.
 

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

Top