DataReader - Invalid attempt to read when no data is present

DanielS

New member
Joined
May 20, 2008
Messages
2
Programming Experience
3-5
Hello everyone! This is my first time posting on this site, and it seems pretty impressive (top of the list from my google search!).

My question is this:

I have a DataReader that returns several rows - let's say hypothetically 30. The problem is that records 23-27 have been deleted.. I want to systematically read through a column from each row, and compare it against a value a user entered in a textbox. If a value matches what the user entered, I set the value in the textbox back to 0. Basically, I want this column to not be able to store duplicates (other then 0). The problem occurs when I attempt to read through the data and hit the rows that do not exist I believe, and I get the following error: InvalidOperationException occured, Invalid attempt to read when no data is present.

I can't check either to see if the DataReader.Item("columnName") has a value in it, or is nothing... it just throws the same error!

(My first guess was to do an If.. then... end if check to see if the column was not nothing, then proceed).

But for wahtever reason, everything I do ends up with the call to .Item("columnName") throwing that error.

Do you have any suggestions, vbdotnetforums people?

Thanks,

Daniel
 
The Read method of the SqlDataReader returns a boolean value that tells you whether a record was read or not. May you just don't check that value.

Basically, you'd go through those records with a loop like this :

VB.NET:
while (myReader.Read())
    'do stuff with the records
end while

I hope this helps...
 
It did thanks! Another thing I added, and what was also causing a problem is that I wasn't jumping out of the loop if the value I was looking for was actually found.

so my while ended up looking like

VB.NET:
dim integerValue as integer = "someValue"

Do While objectInstance.DataReader.Read()
           If integerValue = objectInstance.DataReader.Item("columnName") Then
                            bolColumnValueExists = True
                            Exit Do
           End If
Loop

Thanks alot of the quick reply
 

Latest posts

Back
Top