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(" ")
                Else
                    itmListItem.SubItems.Add(myData.GetString(shtCntr))
                End If
            Next shtCntr
            view_data.DatabaseListView.Items.Add(itmListItem)
        Loop

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?

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

Comments
Thank you!

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!

This question has already been answered. Start a new discussion instead.