如何使用复选框更新数据库中一列中的多个数字 [英] how do update multiple numbers in one column in the database using checkboxes

查看:64
本文介绍了如何使用复选框更新数据库中一列中的多个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所做的,但只会更新我选择的第一个框,并忽略其余框

this is what i have done but it only update the first box i select and ignors the rest

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/");
                        }
                       
                    }
                }
            }
        }
    } 

推荐答案

您可以尝试一次吗?没有重大代码更改.只是尝试在循环的每次迭代中初始化命令对象.

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

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/");
                        }
                       
                    }
                }
            }
        }
    }


这篇关于如何使用复选框更新数据库中一列中的多个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆