2010-02-09 55 views
0

我對'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作爲字符串/日期)後至今

+0

如何檢查列是否爲空? – 2010-02-09 07:44:47

+0

我試過了;如果r.Item(「DateModified」)IsNot DBNull.Value然後datemodified =(r(「DateModified」))Else datemodified = String.Empty結束如果(但似乎沒有工作) – Phil 2010-02-09 08:30:42

回答

1

是否改變

datemodified = (r("DateModified")) 

datemodified = (r("datemodified")) 

工作?

+0

感謝您的幫助,沒有我有它作爲日期修改,然後我改爲DateModified作爲一個嘗試解決這個問題。 – Phil 2010-02-09 07:49:38

1

我試圖通過傳入空值,空集來重新創建問題。我無法在您提供的代碼示例中重新創建它。你介意發佈更多的代碼,甚至整個頁面嗎?有錯誤發生的行號嗎?我想深入挖掘。

相關問題