0

I am having a little trouble with an 'IndexOutOfRangeException'. I think it is because when the code tries to get the DateModified col from the database it sometimes contains NULLS (the users haven't always updated the page since they created it).

Here is my code;

s = ("select datemodified, maintainedby, email, hitcount from updates where id =   @footid")
Dim x As New SqlCommand(s, c)
x.Parameters.Add("@footid", SqlDbType.Int)
x.Parameters("@footid").Value = footid
c.Open()
Dim r As SqlDataReader = x.ExecuteReader
While r.Read
    If r.HasRows Then
        datemodified = (r("DateModified"))
        maintainedby = (r("maintainedby"))
        email = (r("email"))
        hitcount = (r("hitcount"))
    End If
End While
c.Close()

I thought (after a bit of msdn) that adding;

If r.HasRows Then 

End If 

Would solve the problem, but so far after adding I am still getting the same old IndexOutOfRangeException

(ps, I have tried datemodified as string / datetime)

2
  • 1
    How about checking if the column is null? Commented Feb 9, 2010 at 7:44
  • I have tried; If r.Item("DateModified") IsNot DBNull.Value Then datemodified = (r("DateModified")) Else datemodified = String.Empty End If (but didnt seem to work) Commented Feb 9, 2010 at 8:30

3 Answers 3

1

Try r.IsDBNull(columnIndex).

Sign up to request clarification or add additional context in comments.

Comments

1

Does changing

datemodified = (r("DateModified")) 

to

datemodified = (r("datemodified")) 

work?

1 Comment

Thanks for the help, No i did have it as datemodified then I changed to DateModified as an attempt to fix the problem.
1

I have tried to recreate you issue by passing in nulls, empty sets. I can't recreate it in the sample of code you provided. Would you mind posting more code, maye even the whole page? Is there a line number that the error happens on? I would like to dig in further.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.