2012-04-13 55 views
-2

我得到NullReferenceException在下面的代碼中由用戶代碼未處理。NullReferenceException未被用戶代碼未處理 - 未將對象引用設置爲對象的實例

if (item["Area"] != null) 
{ 
    drpArea.Items.FindByText(item["Area"].ToString()).Selected = true;//error occurs here. 
} 

試圖找出100萬次。但我無法做到。

+2

的[什麼是.NET一個NullReferenceException?](HTTP可能重複:// stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – 2012-04-13 04:17:21

+2

我認爲你需要在drpArea.Items.FindByText(item [「Area」]。ToString())中測試null。甚至可能是drpArea的Items屬性。我不知道那個變量引用的類型。 – Roman 2012-04-13 08:59:11

回答

1

可能是你應該首先從Dropdownlist得到listitem比設置選定值

試試這個:

ListItem li = drpArea.Items.FindByText(Convert.ToString(item["Area"])); 
if (li != null) 
    drpArea.SelectedValue = li.Value; 
相關問題