Object reference not set to an instance of an object in c# for RadComboBox
本问题已经有最佳答案,请猛点这里访问。
我正在获取未设置为RadcomboBox对象错误实例的对象引用。当我没有选择任何项目时,出现此错误。请帮帮我。谢谢。
ASPX:
1 2 | <telerik:RadComboBox ID="cmbPrimarySpoc" runat="server" Width="160px" AllowCustomText="true" EmptyMessage="Select Primary SPOC" eAutomaticLoadOnDemand="False"> </telerik:RadComboBox> |
C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | if (cmbPrimarySpoc.SelectedItem.Text !="" && cmbPrimarySpoc.SelectedValue !="Select Primary SPOC") { intPriSpocID = Convert.ToInt32(cmbPrimarySpoc.SelectedValue.ToString()); } else { intPriSpocID = 0; } private void FillPrimarySpocDetails() { try { DataSet dsSource = new DataSet(); BizUser bUser = new BizUser(); RadComboBoxItem cItem = new RadComboBoxItem("Select Primary SPOC","Select Primary SPOC"); cmbPrimarySpoc.Items.Add(cItem); dsSource = bUser.GetAllUsers(Security.AccountID, UserStatus.Active); if (dsSource != null) { if (dsSource.Tables[0].Rows.Count > 0) { for (int i = 0; i <= dsSource.Tables[0].Rows.Count - 1; i++) { cItem = new RadComboBoxItem(dsSource.Tables[0].Rows[i]["UserName"].ToString(), dsSource.Tables[0].Rows[i]["UserID"].ToString()); cmbPrimarySpoc.Items.Add(cItem); } } } } catch { } } |
如果试图访问具有空值的对象实例的成员,则会出现"未设置为对象实例的对象引用"。
添加空支票
1 2 3 4 | if (cmbPrimarySpoc.SelectedItem != null) { //do your process } |
I am getting this error when i didn't select any item.
当不选择任何值时,
根据您的情况添加EDOCX1[1]检查,如下所示:
1 2 3 | if (cmbPrimarySpoc.SelectedValue != null && cmbPrimarySpoc.SelectedItem.Text !="" && cmbPrimarySpoc.SelectedValue !="Select Primary SPOC") { intPriSpocID = Convert.ToInt32(cmbPrimarySpoc.SelectedValue.ToString()); } |