title: How to get cell value from gridview when radio button list item is selected?
body: <p>I have a gridview which is displaying data dynamically from SQL table. No problem with this part. I've added a colunm that contains a radio button list. I would like to get all cells value of Column 3 (index 2) if a radio button list item of these rows is selected when the user click on the Submit button.</p>
  
  <p>My gridview :</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server"&gt;
  &lt;Columns&gt;
  &lt;asp:TemplateField&gt;
      &lt;HeaderTemplate&gt;
          RadioButtonList
      &lt;/HeaderTemplate&gt;
      &lt;ItemTemplate&gt;
          &lt;asp:RadioButtonList ID="Radio1" runat="server"&gt;
              &lt;asp:ListItem Value="1" Text="OK" /&gt;
              &lt;asp:ListItem Value="0" Text="KO" /&gt;
          &lt;/asp:RadioButtonList&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  &lt;asp:Button ID="Submit" runat="server" Text="Submit" OnClick="Submit_Click"/&gt;
  
  protected void Page_Load(object sender, EventArgs e)
  {
      string strSQLconnection = "Connection to DB";
      SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
  
      SqlCommand sqlCommand = new SqlCommand("SELECT Champ1, Champ2, Camp3 FROM Table1 WHERE Condition1 IS NULL AND Condition2 IS NULL", sqlConnection);
  
      sqlConnection.Open();
  
      SqlDataReader reader = sqlCommand.ExecuteReader();
  
      GridView1.DataSource = reader;
      GridView1.DataBind();
  
      sqlConnection.Close();
  }
  </code></pre>
  
  <p>Edit : Gridview example</p>
  
  <pre><code>---------------------------------------------
  Radio   | Column0   | Column1   | Column2   |
  ---------------------------------------------
  °OK °KO | abc       | abc       | abc       |
  ---------------------------------------------
  °OK °KO | abc       | abc       | abc       |
  --------------------------------------------- 
  °OK °KO | abc       | abc       | abc       |
  --------------------------------------------- 
  °OK °KO | abc       | abc       | abc       |
  --------------------------------------------- 
  °OK °KO | abc       | abc       | abc       |
  --------------------------------------------- 
  °OK °KO | abc       | abc       | abc       |
  --------------------------------------------
  </code></pre>
  
  <p>If a radio list item is selected, i would like to get value of the corresponding cell in column1 when clicking on submit button.</p>
  
  <p>My code behind :</p>
  
  <pre><code>protected void Submit_Click(object sender, EventArgs e)
  {
  foreach (GridViewRow row in GridView1.Rows)
  {
      //Find the Radio button control
      RadioButtonList rb = (RadioButtonList)row.FindControl("Radio1");
  
      if (rb.SelectedItem.Text == "OK")
      {
  
          string id = row.Cells[2].Text;
  
          string query = "Query";
  
          SqlConnection con = new SqlConnection("Connection to DB");
          SqlCommand cmd = new SqlCommand(query, con);
  
          con.Open();
          added = cmd.ExecuteNonQuery();
  
          con.Close();
      }
  }
  }
  </code></pre>
  
  <p>The error faced when submitting : </p>
  
  <pre><code>[NullReferenceException: Object reference not set to an instance of an object.]
     MasterPage.Submit_Click(Object sender, EventArgs e) +202
     System.Web.UI.WebControls.Button.OnClick(EventArgs e) +114
     System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +139
     System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +28
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2980
  </code></pre>
  
  <p>Dot know how to handle this.</p>