I keep getting this "Invalid attempt to read when no data is present." when trying to read a value in a SQLDataReader. 500 Points because I'm overdue...
Posted on 2006-05-05
Man, VS.NET 2005 is killin' me. To do the easiest things seem to take 5 times as long as in 2003. Anyway, I have a sql query that returns rows. I *KNOW* there are values in each field. Since there is ZIP for documentation and examples for Reader.GetValue(), I've been spinning wheels. Anytime I go to a M$ *cough* 'Help' page, it is rated 1 out of 10 by about a zillion users.
However, whenever I try to execute this reader, it tells me the above error. Here's the code snippet.
Dim odrreader As SqlDataReader = dbCommand.ExecuteReader()
szstudentid = "0"
If Not (odrreader.HasRows) Then
berrors = true
szlog = "There is no SID or SSN = '" & szsid & "' in the database for row: " & import_iter.ToString() & ". Please investigate."
ltlOutput.Text = ltlOutput.Text & "<BR />" & szlog
'first get the student info we'll need from the students record
szstudentid = odrreader.GetValue(ColumnNameToIndex(odrreader, "student_id")).ToString()
All the ColumnNameToIndex(SQLReader, columnname) does is return the int value (since according to VS.NET 2K5, you can no longer do a reader.ColumnValue(columnname).ToString()) of the column in the reader row fields.
I am behind schedule and don't have time to play for 5 hours to figure out what is wrong. Could someone explain to me why I would get this error from the above code, and how to fix the thing? I'm so desperate that I'm awarding 500 pts. I can find a ZILLION hits when I type in the above error--you know that M$ screwed up when so many people are having the same problem. I can assure you 100% that the odrreader.HasRows() works fine, and that there *is* a value student_id value (it's an Integer) in this row.
Much appreciation (and points) for the solution!