0

我收到错误:对象引用未设置为对象的实例。这里是代码

  <asp:radiobuttonlist id="JAN" runat="server">
              <asp:listitem id="radL" runat="server" value="L" />
              <asp:listitem id="radP" runat="server" value="P" />
              </asp:radiobuttonlist>

后面的代码:

 protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
                    {
                      try
                    {
                      GridEditableItem editedItem = e.Item as GridEditableItem;
                      RadioButtonList JAN = (RadioButtonList)editedItem.FindControl("JAN");
                       string GENDER = JAN.SelectedValue;

                       foreach (ListItem item in JAN.Items)
                       { 
                         if (item.Selected)
                       {
                          GENDER = item.Value;
                        } 
                      } 
                      SqlConnection conn1 = BusinessTier.getConnection();
                      conn1.Open();
                      int flg = BusinessTier.SavePersonalInfo(conn1, 1,JAN.SelectedItem.Value,)
                      BusinessTier.DisposeConnection(conn1);
4

1 回答 1

0

该错误可能是因为,1)您没有检查 RadioButtonList 是否为空。错误是因为您没有获得 RadioButtonList 的引用。检查以下代码:

protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
                {
                  try
                {
                  GridEditableItem editedItem = e.Item as GridEditableItem;
                  RadioButtonList JAN = (RadioButtonList)editedItem.FindControl("JAN");

                 **// add below code**
                  if(JAN==null)
                  {
                     // print some error message..
                     return;
                   }

                   string GENDER = JAN.SelectedValue;

                   foreach (ListItem item in JAN.Items)
                   { 
                     if (item.Selected)
                   {
                      GENDER = item.Value;
                    } 
                  } 
                  SqlConnection conn1 = BusinessTier.getConnection();
                  conn1.Open();
                  int flg = BusinessTier.SavePersonalInfo(conn1, 1,JAN.SelectedItem.Value,)
                  BusinessTier.DisposeConnection(conn1);

2)并且您已将所选项目值直接传递给某个函数。

int flg = BusinessTier.SavePersonalInfo(conn1, 1,JAN.SelectedItem.Value,)

但是可能会选择多个项目。

于 2012-11-20T11:20:33.460 回答