0

I want to update a column of a selected row in gridview after selecting radiobutton. Forexample, I have Candidiate table with ID, Name, Address, Email, Phone, Status etc. Once I select radiobutton, it ties to ID. Then, based on ID, I want to change the record where Status column is assigned either "Y" or "N". I have achieved tying down radiobutton with ID. After that, I am lost to identifying ID with correct record row. Please help me advise with sample codes.

Thank in advance. Joe Below is my html code.

 <asp:GridView ID="gvCandidates" runat="server" AutoGenerateColumns="False" 
    BackColor="Green" BorderColor="#CC9966" BorderStyle="None" AlternatingRowStyle-BackColor = "#C2D69B"
    BorderWidth="1px" CellPadding="4" AllowPaging = "true">
    <Columns>
       <asp:TemplateField HeaderText="Select One">
    <ItemTemplate>
      <input name="SelectedRadioButton" type="radio" 
                value='<%# Eval("CANDIDATE_ID") %>' />
    </ItemTemplate>
  </asp:TemplateField>
        <asp:BoundField DataField="CANDIDATE_ID" HeaderText="CANDIDATE_ID" 
            SortExpression="CANDIDATE_ID" />
        <asp:BoundField DataField="CANDIDATE_NAME" HeaderText="CANDIDATE_NAME" 
            SortExpression="CANDIDATE_NAME" />
        <asp:BoundField DataField="CANDIDATE_ADDRESS1" HeaderText="CANDIDATE_ADDRESS1" 
            SortExpression="CANDIDATE_ADDRESS1" />
        <asp:BoundField DataField="CANDIDATE_PHNO" HeaderText="CANDIDATE_PHNO" 
            SortExpression="CANDIDATE_PHNO" />
        <asp:BoundField DataField="CANDIDATE_EMAIL_ADDRESS" 
            HeaderText="CANDIDATE_EMAIL_ADDRESS" SortExpression="CANDIDATE_EMAIL_ADDRESS" />
        <asp:BoundField DataField="CANDIDATE_STATUS" HeaderText="CANDIDATE_STATUS" 
            SortExpression="CANDIDATE_STATUS" />
    </Columns>
</asp:GridView>

Below is my c# code.

protected void btnAccepted_Click(object sender, EventArgs e)
{
  string CStatus = "Y";
  string selectedValueID = Request.Form["SelectedRadioButton"];
  lblMsgStatus.Visible = true;
  lblMsgStatus.Text = selectedValueID;


    compCon = new SqlConnection(strConnectionString);
    compCon.Open();
    string UpdateQuery = @"UPDATE CandidateDetails SET CANDIDATE_STATUS='Y' WHERE CANDIDATE_ID='@selectedValueID'";
    SqlCommand UpdateCandidateCommand = new SqlCommand(UpdateQuery, compCon);
    UpdateCandidateCommand.Connection = compCon;

    UpdateCandidateCommand.ExecuteNonQuery();
    compCon.Close();
    }
3
  • Do you want to update on selection of radio button for single record or update for all records on button Accepted click?
    – GMD
    Jul 23, 2014 at 12:04
  • I want to update a single record by clicking Accepted Button after selecting a single record via radio button.
    – jkhaung
    Jul 23, 2014 at 16:08
  • You can loop through gridview & with datakey you can get for which record radio button selection is made & then save data.
    – GMD
    Jul 24, 2014 at 4:01

2 Answers 2

0

See below, you can get id as shown in Test button click:

    <asp:GridView ID="gv" AllowPaging="True" PageSize="10" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_RowDataBound" 
DataKeyNames="TestId">
        <Columns>
         <asp:BoundField HeaderText="Test" DataField="TestId" />
         <asp:TemplateField>
          <ItemTemplate>
            <asp:RadioButton ID="rbtn1" runat="server" Text="Yes" />
            <asp:RadioButton ID="rbtn2" runat="server" Text="No" />
          </ItemTemplate>
         </asp:TemplateField>
        </Columns>
        </asp:GridView>
        <asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" />


protected void btnTest_Click(object sender, EventArgs e)
{
 foreach (GridViewRow gvrow in gv.Rows)
 {
   string theDataKey = gv.DataKeys[gvrow.RowIndex].Value.ToString();//Id field
   RadioButton rbtn1 = gvrow.FindControl("rbtn1") as RadioButton;
   RadioButton rbtn2 = gvrow.FindControl("rbtn2") as RadioButton;
   string YesNo = "N";//set default value
   if(rbtn1.Checked)
   YesNo = "Y";
   else if(rbtn2.Checked)
   YesNo = "N";
  }
} 
8
  • Thank. Let me try with your code. Thank you in millions.
    – jkhaung
    Jul 24, 2014 at 5:53
  • The code is not working. In debugging, at string theDataKey, the index is out of range. Seem like index of selected row is not read. Please advise. Thank.
    – jkhaung
    Jul 26, 2014 at 4:02
  • Add your Id field as data key for Gridview :DataKeyNames="TestId"
    – GMD
    Jul 26, 2014 at 4:04
  • Still not working. At "if" statement if (rbtn.checked), the error shows object is not created properly. I think that object was not created properly in radiobutton and I dont know how to create.
    – jkhaung
    Jul 29, 2014 at 5:57
  • What Id you gave to Radio buttons for Yes/No?
    – GMD
    Jul 29, 2014 at 5:59
0

you should to use:

string UpdateQuery = @"UPDATE CandidateDetails SET CANDIDATE_STATUS='Y' WHERE CANDIDATE_ID IN ( [SELECTED VALUES]);

and in order [SELECTED VALUES] you should to place comma seperated values
for example (1,2,4,7,9) or ('a','f','amita','david')

2
  • Hi Farrokh, I still don't get what you are trying to say. Do you want me to replace SELECTED VALUES with variable name SelectedValueID. If so, it is still not working. Any idea, please.... thank so much for your help. I am just a begineer for asp .net.
    – jkhaung
    Jul 23, 2014 at 16:10
  • I think you want to update multiple record together,so yo should to get his records key field(for example id) and seperate them with comma, then put them inside the ().understand ?
    – Farrokh
    Jul 24, 2014 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.