我對'IndexOutOfRangeException'有點麻煩。我認爲這是因爲當代碼嘗試從數據庫中獲取DateModified col時,它有時包含NULLS(用戶並不總是在創建頁面後更新頁面)。如何避免IndexOutOfRangeException索引DataReader列?
這是我的代碼;
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()
我以爲(後一點msdn),添加;
If r.HasRows Then
End If
可以解決這個問題,但加入我仍然得到同樣的老IndexOutOfRangeException
(PS,我已經試過datemodified作爲字符串/日期)後至今
如何檢查列是否爲空? – 2010-02-09 07:44:47
我試過了;如果r.Item(「DateModified」)IsNot DBNull.Value然後datemodified =(r(「DateModified」))Else datemodified = String.Empty結束如果(但似乎沒有工作) – Phil 2010-02-09 08:30:42