Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is what i have done but it only update the first box i select and ignors the rest
C#
protected void btncast_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder(string.Empty);
            SqlConnection con = new SqlConnection(strConnection);
            SqlCommand cmd = new SqlCommand();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkselect = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkselect");
                if (chkselect != null)
                {
                    if (chkselect.Checked)
                    {
                        string strID = GridView1.Rows[i].Cells[0].Text;
        
                        try
                        {
                            const string strUpdate = "Update Candidate_list set total_counts = total_counts+1 WHERE pic_id = @pic_id";
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = strUpdate.ToString();
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@pic_id", strID);
                            cmd.Connection = con;
                            con.Open();
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException ex)
                        {
                            string errorMsg = "Error in Updation";
                            errorMsg += ex.Message;
                            throw new Exception(errorMsg);
                        }
                      finally
                        {
                            con.Close();
                            Response.Redirect("~/Voting.aspx/");
                        }
                       
                    }
                }
            }
        }
    } 
Posted

1 solution

Can you try this once? No major code change. just trying to initialize command object in each iteration of the loop.

C#
protected void btncast_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder(string.Empty);
            SqlConnection con = new SqlConnection(strConnection);
            SqlCommand cmd;
 
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkselect = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkselect");
                if (chkselect != null)
                {
                    if (chkselect.Checked)
                    {
                        string strID = GridView1.Rows[i].Cells[0].Text;
        
                        try
                        {
                            cmd = new SqlCommand();
                            const string strUpdate = "Update Candidate_list set total_counts = total_counts+1 WHERE pic_id = @pic_id";
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = strUpdate.ToString();
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@pic_id", strID);
                            cmd.Connection = con;
                            con.Open();
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException ex)
                        {
                            string errorMsg = "Error in Updation";
                            errorMsg += ex.Message;
                            throw new Exception(errorMsg);
                        }
                      finally
                        {
                            con.Close();
                            Response.Redirect("~/Voting.aspx/");
                        }
                       
                    }
                }
            }
        }
    }
 
Share this answer
 
v3

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