sqlreader.GetValue(sqlreader.GetOrdinal(columnname) problem

G

Guest

Hi all,
I am stuck with this problem.
the code is like this:
Dim sqlreader As SqlDataReader =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)

sqlreader.GetValue(sqlreader.GetOrdinal(columnname) give me the following in
the watch window:

sqlreader.GetValue(sqlreader.GetOrdinal("enrolldate")) Run-time exception
thrown : System.InvalidOperationException - Invalid attempt to read when no
data is present.
But I can get the column number: sqlreader.GetOrdinal("enrolldate") is 1.
What's going on?
Thank you.
Betty
 
B

Brian

Do you call sqlreader.Read() before trying to get a value? No rows are
fetched until you do this.
 
G

Guest

When I look at the sqlreader in the watch window, it gave the following info:

Depth 0 Integer
FieldCount 62 Integer
HasRows True Boolean
IsClosed False Boolean
Item <cannot view indexed property> Object
RecordsAffected -1 Integer

So that means I pull the data through the procedure, right.
 
G

Guest

Enclose your code in a while loop like this:

While sqlreader.Read()
'place the code to GetValue here
End While
 
G

Guest

thank you all. I missed that part .
--
Betty


sloan said:
As other people has previous posted:
http://www.lhotka.net/cslacvs/viewcvs.cgi/*checkout*/CSLA10/CSLA/SafeDataReader.vb

this SafeDataReader has these built in checks for you.

But... yes, you have to call the Read() method ... and put your code in the
Read() loop ....

Its kinda like "If not rs.EOF".. but its not quite that exactly either.

...

The SafeDataReader is a nice bit of code to make the IDataReader a little
easier to deal with.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top