title: Checkbox check is not working
body: <p>I have 4-5 <code>checkbox</code> inside gridview. I want to save its value into the database like, </p>
  
  <p>If <code>checked == true</code>, then <code>"Y"</code> else <code>"N"</code></p>
  
  <p>but got error as</p>
  
  <blockquote>
    <p>object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>I defined all checkboxes as below</p>
  
  <pre><code>GridDataControlFieldCell chkcellAdd = GrdRights.RowsInViewState[i].Cells[4] as GridDataControlFieldCell;
  GridDataControlFieldCell chkcellView = GrdRights.RowsInViewState[i].Cells[5] as GridDataControlFieldCell;
  GridDataControlFieldCell chkcellEdit= GrdRights.RowsInViewState[i].Cells[6] as GridDataControlFieldCell;
  GridDataControlFieldCell chkcellDelete = GrdRights.RowsInViewState[i].Cells[7] as GridDataControlFieldCell;
  
  CheckBox chkadd = chkcellAdd.FindControl("ChkIDAdd") as CheckBox;
  CheckBox chkview = chkcellView.FindControl("ChkIDView") as CheckBox;
  CheckBox chkedit = chkcellEdit.FindControl("ChkIDEdit") as CheckBox;
  CheckBox chkdelete = chkcellDelete.FindControl("ChkIDDelete") as CheckBox;
  </code></pre>
  
  <p>I tried a link from <a href="https://stackoverflow.com/questions/5743099/looping-through-gridview-rows-and-checking-checkbox-control">here</a> but getting the same error.</p>
  
  <p>Please suggest what is wrong here</p>
  
  <p>HTML of my gridview:-</p>
  
  <pre><code>&lt;cc2:Grid ID="GrdRights" runat="server" FolderStyle="../Styles/Grid/style_12"
      AllowSorting="False" AutoGenerateColumns="False" AllowColumnResizing="true" AllowAddingRecords="false"
      AllowMultiRecordSelection="true" OnRowDataBound="GrdRights_RowDataBound" ViewStateMode="Enabled"
      PageSize="100"&gt;
      &lt;ClientSideEvents OnClientSelect="FunMonthList" /&gt;
      &lt;ScrollingSettings ScrollHeight="400px" /&gt;
      &lt;Columns&gt;
          &lt;cc2:Column ID="Column1" DataField="MKEY" ShowHeader="false" HeaderText="Select" ReadOnly="true"
              Width="5%" runat="server"&gt;
              &lt;TemplateSettings TemplateId="TemplateWithCheckbox" /&gt;
          &lt;/cc2:Column&gt;
  
          &lt;cc2:Column ID="Column2" DataField="parent_menu" HeaderText="MENU" Visible="true"
              ReadOnly="true" Width="10%" runat="server"&gt;
          &lt;/cc2:Column&gt;
          &lt;cc2:Column ID="Column4" DataField="child_menu_mkey" Visible="false" ReadOnly="true"
              Width="10%" runat="server"&gt;
          &lt;/cc2:Column&gt;
          &lt;cc2:Column ID="Column3" DataField="child_menu" HeaderText="SUB MENU" runat="server"
              Visible="true" Width="30%"&gt;
          &lt;/cc2:Column&gt;
  
            &lt;%--&lt;cc2:Column ID="Column5" DataField="MKEY" ShowHeader="false" HeaderText="Select"                                                  ReadOnly="true" Width="5%" runat="server"&gt;
              &lt;TemplateSettings TemplateId="TemplateWithCheckbox" /&gt;
          &lt;/cc2:Column&gt;--%&gt;
  
          &lt;cc2:CheckBoxColumn Id="chkAdd" DataField="ADD_FLAG" ShowHeader="true" HeaderText="Add"
              Width="5%" runat="server" Align="right"&gt;
              &lt;TemplateSettings TemplateId="TemplateWithChecAdd"  /&gt;
          &lt;/cc2:CheckBoxColumn&gt;
  
          &lt;cc2:CheckBoxColumn Id="chkEdit" DataField="MODIFY_FLAG" ShowHeader="true" HeaderText="Edit"
              Width="5%" runat="server" Align="right"&gt;
              &lt;TemplateSettings TemplateId="TemplateWithChecEdit" /&gt;
          &lt;/cc2:CheckBoxColumn&gt;
  
          &lt;cc2:CheckBoxColumn Id="ChkView" DataField="VIEW_FLAG" ShowHeader="true" HeaderText="View"
              Width="6%" runat="server" Align="right"&gt;
              &lt;TemplateSettings TemplateId="TemplateWithChecView" /&gt;
          &lt;/cc2:CheckBoxColumn&gt;
  
          &lt;cc2:CheckBoxColumn Id="ChkDelete" DataField="DEL_FLAG" ShowHeader="true" HeaderText="Delete"
              Width="8%" runat="server" Align="right"&gt; 
              &lt;TemplateSettings TemplateId="TemplateWithChecDelete" /&gt;
          &lt;/cc2:CheckBoxColumn&gt;
  
      &lt;/Columns&gt;
      &lt;Templates&gt;
  
          &lt;cc2:GridTemplate ID="TemplateWithChecAdd"&gt;
              &lt;Template&gt;
                  &lt;asp:CheckBox runat="server" ID="ChkIDAdd" ToolTip="&lt;%# Container.Value %&gt;"  /&gt;
              &lt;/Template&gt;
          &lt;/cc2:GridTemplate&gt;
  
          &lt;cc2:GridTemplate ID="TemplateWithChecView"&gt;
              &lt;Template&gt;
                  &lt;asp:CheckBox runat="server" ID="ChkIDView" ToolTip="&lt;%# Container.Value %&gt;" /&gt;
              &lt;/Template&gt;
          &lt;/cc2:GridTemplate&gt;
  
          &lt;cc2:GridTemplate ID="TemplateWithChecEdit"&gt;
              &lt;Template&gt;
                  &lt;asp:CheckBox runat="server" ID="ChkIDEdit" ToolTip="&lt;%# Container.Value %&gt;" /&gt;
              &lt;/Template&gt;
          &lt;/cc2:GridTemplate&gt;
  
          &lt;cc2:GridTemplate ID="TemplateWithChecDelete"&gt;
              &lt;Template&gt;
                  &lt;asp:CheckBox runat="server" ID="ChkIDDelete" ToolTip="&lt;%# Container.Value %&gt;" /&gt;
              &lt;/Template&gt;
          &lt;/cc2:GridTemplate&gt;
  
      &lt;/Templates&gt;
  &lt;/cc2:Grid&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>AS per my code now, I got the checkbox values, but the data is not getting inserted into the table and i am getting error as</p>
  
  <blockquote>
    <p>Column name 'ADD_FLAG' appears more than once in the result column list.
    Msg 264, Level 16, State 1, Line 1</p>
    
    <p>Column name 'MODIFY_FLAG' appears more than once in the result column list.
    Msg 264, Level 16, State 1, Line 1</p>
    
    <p>Column name 'VIEW_FLAG' appears more than once in the result column list.
    Msg 264, Level 16, State 1, Line 1</p>
    
    <p>Column name 'DEL_FLAG' appears more than once in the result column list.</p>
  </blockquote>
  
  <p>Below is my code:-</p>
  
  <pre><code>public bool Save()
  {
      try
      {
          for (int i = 0; i &lt; GrdRights.RowsInViewState.Count; i++)
          {
              string strSQLMKEY = "SELECT WMS_User_Rights.MKEY  FROM WMS_User_Rights Inner JOIN WMS_Menu_Rights on " +
                                         "WMS_User_Rights.User_Id = WMS_Menu_Rights.Mkey " +
                                         "where WMS_User_Rights.User_Id='" + Hid_Selected_user.Value + "' " +
                                         "AND WMS_User_Rights.DELETE_FLAG = 'N'";
              if (GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim().ToString() == "0")
              {
                  strSQLMKEY += " and MENU_MKEY='" + GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("MKEY")].Text.Trim() + "'";
              }
              else
              {
                  strSQLMKEY += " and MENU_MKEY='" + GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim() + "'";
              }
              con.Open();
              SqlCommand cmdMKEY = new SqlCommand(strSQLMKEY, con);
              if (cmdMKEY.ExecuteScalar() != null)
              {
                  strMode = "M";
                  iMKey = Convert.ToInt32(cmdMKEY.ExecuteScalar());
              }
              else
              {
                  strMode = "A";
                  iMKey = 0;
              }
              con.Close();
              System.Text.StringBuilder StrPubBldg = new System.Text.StringBuilder();
              XmlWriter xw = XmlWriter.Create(StrPubBldg);
              xw.WriteStartElement("DocumentElement");
              {
                  xw.WriteStartElement("WMS_Menu_Rights");
                  for (int j = 0; j &lt; GrdRights.RowsInViewState.Count; j++)
                  {
                      bool str_checkadd = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[4]).FindControl("ChkIDAdd")).Checked;
                      bool str_checkEdit = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[5]).FindControl("ChkIDEdit")).Checked;
                      bool str_checkView = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[6]).FindControl("ChkIDView")).Checked;
                      bool str_checkdel = ((CheckBox)((GridDataControlFieldCell)GrdRights.RowsInViewState[j].Cells[7]).FindControl("ChkIDDelete")).Checked;
  
                      xw.WriteElementString("ADD_FLAG", str_checkadd == true ? "Y" : "N");
                      xw.WriteElementString("MODIFY_FLAG", str_checkEdit == true ? "Y" : "N");
                      xw.WriteElementString("VIEW_FLAG", str_checkView == true ? "Y" : "N");
                      xw.WriteElementString("DEL_FLAG", str_checkdel == true ? "Y" : "N");
  
                      if (GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim().ToString() == "0")
                      {
                          xw.WriteElementString("MENU_MKEY", GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("MKEY")].Text.Trim());
                      }
                      else
                      {
                          xw.WriteElementString("MENU_MKEY", GrdRights.Rows[i].Cells[GrdRights.Columns.GetColumnIndexByDataField("child_menu_mkey")].Text.Trim());
                      }
                      xw.WriteElementString("USER_ID", Hid_Selected_user.Value);
                      xw.WriteElementString("DELETE_FLAG", "N");
                      xw.WriteElementString("CREATION_DATE", System.DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"));
                  }
  
                  xw.WriteEndElement();
                  xw.Close();
                  MainEnqMkey = InsertUpdateDelete.InsertUpdateDeleteCls.InsertUpdateDelete_sql(strMode, Convert.ToInt16(iMKey), "WMS_Menu_Rights", "MKEY", "MUR", StrPubBldg.ToString());
              }
  
          }
          if (MainEnqMkey.Equals(0))
          {
              ClientScript.RegisterStartupScript(this.GetType(), "SuccessScript", "alert('Some Error Occured While Saving Data !!')", true);
          }
          else
          {
              con.Open();
              SqlCommand ObjPriCmd = new SqlCommand("delete from WMS_User_rights where MKEY=" + HidTempMkey.Value, con);
              ObjPriCmd.ExecuteNonQuery();
              con.Close();
  
              if (!Directory.Exists(Server.MapPath(StrFolder)))
              {
                  Directory.CreateDirectory(Server.MapPath(StrFolder));
              }
              if (File.Exists(Server.MapPath(StrFolder + StrFileName)) == false)
              {
                  using (System.IO.StreamWriter sw = File.CreateText(Server.MapPath(StrFolder + StrFileName)))
                  {
                      sw.WriteLine("\n");
  
                      sw.WriteLine("\n"); sw.NewLine = "\n------------------------------------------------------------------";
                      sw.WriteLine("\n");
                      sw.Close();
                      sw.Dispose();
                  }
              }
              else
              {
                  using (System.IO.StreamWriter sw = File.AppendText(Server.MapPath(StrFolder + StrFileName)))
                  {
  
                      sw.WriteLine("\n");
  
                      sw.WriteLine("\n"); sw.NewLine = "------------------------------------------------------------------";
                      sw.WriteLine("\n");
                      sw.Close();
                      sw.Dispose();
                  }
              }
          }
          return true;
      }
      catch (Exception ex)
      {
          if (!Directory.Exists(Server.MapPath(StrFolder)))
          {
              Directory.CreateDirectory(Server.MapPath(StrFolder));
          }
          if (File.Exists(Server.MapPath(StrFolder + StrFileName)) == false)
          {
              using (System.IO.StreamWriter sw = File.CreateText(Server.MapPath(StrFolder + StrFileName)))
              {
                  sw.WriteLine("\n");
  
                  sw.WriteLine("\n"); sw.NewLine = "\n------------------------------------------------------------------";
                  sw.WriteLine("\n");
                  sw.Close();
                  sw.Dispose();
              }
          }
          else
          {
              using (System.IO.StreamWriter sw = File.AppendText(Server.MapPath(StrFolder + StrFileName)))
              {
  
                  sw.WriteLine("\n");
  
                  sw.WriteLine("\n"); sw.NewLine = "------------------------------------------------------------------";
                  sw.WriteLine("\n");
                  sw.Close();
                  sw.Dispose();
              }
          }
          return false;
      }
  
      finally
      {
  
      }
  
  }
  
  protected void CmdSave_Click(object sender, EventArgs e)
  {
      if (Save() == true)
      {
          fillGrid();
          if (strMode == "M")
          {
              ClientScript.RegisterStartupScript(this.GetType(), "CloseScript", "alert('Record Modified Successfully');window.location.href='Frm_User_Rights.aspx?TranType=MUR&amp;Mode=A&amp;Key=0&amp;PView=N&amp;userid=" + Request.QueryString["userid"].ToString() + "';", true);
          }
          else
          {
              ClientScript.RegisterStartupScript(this.GetType(), "CloseScript", "alert('Record Saved Successfully');window.location.href='Frm_User_Rights.aspx?TranType=MUR&amp;Mode=A&amp;Key=0&amp;PView=N&amp;userid=" + Request.QueryString["userid"].ToString() + "';", true);
          }
      }
  }
  </code></pre>