문제

i get the error: Object reference not set to an instance of an object. and here the code

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

code behind:

 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);
도움이 되었습니까?

해결책

The error might be because of, 1) you have not checked if the RadioButtonList is null or not. And the error is because of you are not getting reference of RadioButtonList. check below code:

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) and you have passed selected item value to some function directly.

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

But there can be case that multiple items are selected.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top