I am having a problem with a datareader.

Do While myData.Read
            itmListItem = New ListViewItem()
            strValue = IIf(myData.IsDBNull(0), "", myData.GetValue(0))
            itmListItem.Text = strValue

            For shtCntr = 1 To myData.FieldCount() - 1
                If myData.IsDBNull(shtCntr) Then
                    itmListItem.SubItems.Add(" ")
                End If
            Next shtCntr

I have used message boxes for proofing. The code will make it through 6 loops and then throw the specified cast is not valid. It will pass the else, but I think the problem is in the if.

Does anyone have any clue?

Recommended Answers

All 3 Replies

The most common situation is assumming that the field is a string field, but is not.

Please, verify in the DB definition the field type, and use the apropiate get.
If you are unsure, using the GetValue(shtCntr).ToString (instead of Getstring(chtCntr))will try to get the field value, and then convert it to string.

Hope this helps

commented: Thank you! +1

Wow, thank you. That was the error. It was returning a boolean, but wasn't converting it correctly. Thank you so much for your time!

Please, be so kind to mark the thread as solved. :)

commented: Marked as solved, sorry for the delay. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.