Invalid attempt to read when no data is present.

  • Thread starter Thread starter William
  • Start date Start date
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
 
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
 
Back
Top