Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one

I have Gridview with check box. when check box is checked, if I click submit button only checked checkboxes should be updated to 1 into my database . by default my checkbox is 0. Currect is updating everything including unchecked checkboxes

Any help or suggestion is welcome.

Regards

What I have tried:

C#
protected void Button8_Click(object sender, EventArgs e)
        {
           SqlConnection con = new SqlConnection();
            con.ConnectionString = WebConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
            con.Open();
                      


            for (int innercounter = 0; innercounter < GridView1.Rows.Count; innercounter++)
 {
CheckBox chkSelect;
chkSelect = (CheckBox)GridView1.Rows[innercounter].Cells[0].FindControl("RadioButton3");
if (chkSelect.Checked == true)
  {
 
   // RadioButton Field1 = (RadioButton )GridView1.Rows[innercounter].Cells[0].FindControl("RadioButton3");
      SqlCommand cmd = new SqlCommand("UPDATE MCALLERS SET BusinessCallYN ='1' Where CallID=@CallID", con);
                         cmd.ExecuteNonQuery();
    
  }

}
            con.Close();
Posted
Updated 18-Mar-16 3:31am
v2

C#
Your command string contains a parameter:

SqlCommand cmd = new SqlCommand("UPDATE MCALLERS SET BusinessCallYN ='1' Where CallID=@CallID", con);

You never assign a value to the parameter.
 
Share this answer
 
Hi guys thanks I managed to get the solution .

Thanks to Micheal_Davies for response

Below is my solution and is working perfect for me




C#
SqlConnection con = new SqlConnection();
           con.ConnectionString = WebConfigurationManager.ConnectionStrings["NKAccess"].ConnectionString;
           con.Open();

           foreach (GridViewRow Grow in GridView1.Rows)
           {
               RadioButton chselect = (RadioButton)Grow.FindControl("RadioButton3");
               Label lblId = (Label)Grow.FindControl("lblID");


               if (chselect.Checked)
               {


                   string query = "UPDATE MCALLERS SET BusinessCallYN ='1' Where CallID='" + lblId.Text + "'";
                SqlCommand    cmd = new SqlCommand(query, con);
                   cmd.ExecuteNonQuery();
               }
           }

           con.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900