Invalid attempt to read when no data is present

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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.
 
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.
 
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.
 
What you can do is check the DataReader's HasRows property. Then you
can handle the problem gracefully.
 
Back
Top