How to fix Index out of range exception on idatareader

Posted by Phil on Stack Overflow See other posts from Stack Overflow or by Phil
Published on 2010-05-28T09:37:24Z Indexed on 2010/05/28 9:41 UTC
Read the original article Hit count: 776

Good morning stack overflow,

I have been forced into using the idatareader as opposed to the sqldatareader which has .hasrows available.

In my code I am attempting to handle nulls like this:

 reader = GetContent(pageid)
        While reader.Read
            If reader("content") IsNot DBNull.Value Then
                content = Replace(reader("content"), Chr(38) + Chr(97) + Chr(109) + Chr(112) + Chr(59) + Chr(98) + Chr(104) + Chr(99) + Chr(112) + Chr(61) + Chr(49), "")
                If reader("id") IsNot DBNull.Value Then
                    contentid = reader("id")
                End If
            Else
                contentid = -1
                content = String.Empty
            End If
        End While
        Outputcontent.Text = content

I am getting an 'Index Out of Range Exception' here:

If reader("content") IsNot DBNull.Value Then

The database does 100% contain 'content'

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about null