[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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
9
Medium Priority
?
212 Views
Last Modified: 2010-04-23
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()
          'Dim ostudentrs

          szstudentid = "0"
          If Not (odrreader.HasRows) Then
            'error!
            berrors = true
            szlog = "There is no SID or SSN = '" & szsid & "' in the database for row: " & import_iter.ToString() & ". Please investigate."
            ologfile.Log(szlog)
            ltlOutput.Text = ltlOutput.Text & "<BR />" & szlog
          Else
            'first get the student info we'll need from the students record
            szstudentid = odrreader.GetValue(ColumnNameToIndex(odrreader, "student_id")).ToString()
          End if

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!
0
Comment
Question by:lunch_is_4_wimps
  • 4
  • 2
7 Comments
 
LVL 10

Expert Comment

by:vadim63
ID: 16616578
You should call odrreader.Read() before calling GetValue(..).

0
 
LVL 10

Accepted Solution

by:
vadim63 earned 2000 total points
ID: 16616589
Or
while (odrreader.Read())
szstudentid = odrreader.GetValue(ColumnNameToIndex(odrreader, "student_id")).ToString()
0
 
LVL 3

Expert Comment

by:maidinhtai
ID: 16616649
    Dim odrreader As SqlDataReader = dbCommand.ExecuteReader()
          'Dim ostudentrs

          szstudentid = "0"
          If Not (odrreader.HasRows) Then
            'error!
            berrors = true
            szlog = "There is no SID or SSN = '" & szsid & "' in the database for row: " & import_iter.ToString() & ". Please investigate."
            ologfile.Log(szlog)
            ltlOutput.Text = ltlOutput.Text & "<BR />" & szlog
          Else
            odrreader.Read() 'This statement move the cursor to the next record. When dbCommand.ExecuteReader() is called, it return a datareader with the cursor is not points to the first record. When it is called the first time, the cursor points to the first record, and the 2nd time, it points to the 2nd record...
            'first get the student info we'll need from the students record
            szstudentid = odrreader.GetValue(ColumnNameToIndex(odrreader, "student_id")).ToString()
          End If
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:vadim63
ID: 16693259
Did "odrreader.Read()" help?
0
 

Author Comment

by:lunch_is_4_wimps
ID: 16953446
Hey:

I guess I'd like to have this question abandoned.  No one has been able to answer it.  And thank you so much for referring me to the 'how to close a question' page.  I had been trying to find that for months! ;-)
0
 
LVL 10

Expert Comment

by:vadim63
ID: 16954087
What's wrong with "odrreader.Read()"? You didn't post any comments since you got an answer. And I guess there's a correct answer.
0
 

Author Comment

by:lunch_is_4_wimps
ID: 16979952
Sorry.  I had personal issues come up and I had to deal with them.  Sorry I took so long to respond.  And thank you so much for you answer Vadim!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question