sqlreader.GetValue(sqlreader.GetOrdinal(columnname) problem

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Do you call sqlreader.Read() before trying to get a value? No rows are
fetched until you do this.
 
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.
 
Enclose your code in a while loop like this:

While sqlreader.Read()
'place the code to GetValue here
End While
 
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

Similar Threads

Retrieve Record Count 2
xmldatareader? 3
How to reuse sqldatareader? 3
Invalid attempt to read when no data is present 3
get values from query 2
Movenext and ASP.net 5
Closing a datareader 4
Closed DataReader 3

Back
Top