2014-02-14 23 views
2

我有以下錯誤:處理空值引用的DatePicker

System.NullReferenceException: Object reference not set to an instance of an object.

我的編碼是:

If (DirectCast(drv.Cells(4).FindControl("rdp_clsDate"), RadDatePicker).DbSelectedDate).ToString <> "" Then 
    Dim ClsDate As Date = DirectCast(drv.Cells(4).FindControl("rdp_clsDate"), RadDatePicker).DbSelectedDate 
    aa_ml.closeDate = ClsDate.ToString("dd/MM/yyyy") 
Else 
    aa_ml.closeDate = "" 
End If 

這裏CloseDate不是強制性的。所以我可能會放棄,或者我可能不會放棄。當我給出結束日期代碼正常工作,記錄存儲在表中。但是,當我沒有給出結束日期時,我無法執行該程序。在調試時,執行停在此線

If (DirectCast(drv.Cells(4).FindControl("rdp_clsDate"), RadDatePicker).DbSelectedDate).ToString <> "" Then 

我得到了上述錯誤。

如何更改代碼以處理當我沒有給closedate時的情況?

我使用oracle作爲後端。

回答

2

你打電話ToString()DbSelectedDate如果沒有日期是在你的日期選擇器選擇的是一個空場。

把它分成兩行,運行ToString()

+0

嗨ekad之前增加對空支票,謝謝verymuch。它工作完美。 – Belinda

+0

嗨,現在我不能問問題。我該怎麼做..任何人都請引導我 – Belinda

1

在將其轉換爲字符串之前,您需要檢查RadDatePicker.DbSelectedDate Property是否爲Nothing。試試這個代碼

Dim rdp_clsDate As RadDatePicker = DirectCast(drv.Cells(4).FindControl("rdp_clsDate"), RadDatePicker) 
If rdp_clsDate.DbSelectedDate IsNot Nothing AndAlso rdp_clsDate.DbSelectedDate.ToString() <> "" Then 
    Dim ClsDate As Date = rdp_clsDate.DbSelectedDate 
    aa_ml.closeDate = ClsDate.ToString("dd/MM/yyyy") 
Else 
    aa_ml.closeDate = "" 
End If