471,319 Members | 1,211 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Invalid attempt to read when no data is present

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
Dec 21 '05 #1
4 12139
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.

Dec 21 '05 #2
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.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
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

Dec 21 '05 #3
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.

"Chris Dunaway" wrote:
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.

Dec 21 '05 #4
What you can do is check the DataReader's HasRows property. Then you
can handle the problem gracefully.

Dec 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by mosipenko | last post: by
6 posts views Thread by =?Utf-8?B?ZWdzZGFy?= | last post: by

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.