Invalid attempt to read when no data is present.

W

William

Hi,

i'm run follow code:

strQuery = "SELECT MAX(Drug_ID) AS MaxIDNo FROM Drug"
sqlCom = New SqlCommand(strQuery, conn)
drDataReader = sqlCom.ExecuteReader
--> MaxIDNo = DataReader(0)

i get the error -

Additional information: Invalid attempt to read when no data is present.

what the problem about this?

thx
William
 
W

William Ryan

If you just need one value, I'd recommend using
cmd.ExecuteScalar instead of executeReader although they
essentially are doing the same thing. ExecuteScalar is
made to return just one value.

so you could use

Dim i as Integer = sqlCom.ExecuteScalar instead.

--> MaxIDNo = DataReader(0)

If you must use a Reader, use the following, you'll need
to actually iterate through the reader to get any values.
In this case, you'll only have one row but the point is
the same.


Use While dr.Read
MaxIDNo = DataReader(0)
End While


Also, if you are using VS.NET 2003, the dataReader has a
property HasRows to indicate somethign was returned. If
not, no need to use the dr.Read

You can also use the NextRow to advance to the next
position.

Hopefully this helps.

Good Luck,

Bill


W.G. Ryan
(e-mail address removed)
www.knowdotnet.com
 

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