请帮助!我想使用复选框值进行更新 [英] plz help! i want to update using checkbox value

查看:65
本文介绍了请帮助!我想使用复选框值进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用复选框将其递增为1来更新整数值,它可以运行,但不会更新该值.

I want to update an integer value by incrementing with value of 1 using a checkbox by checking it, it runs but it doesn''t update the value.

    //code behind the button
    StringCollection idCollection = new StringCollection();
    string  strid = string .Empty ;
    //loop through gridview to find checked rows
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        CheckBox chckupdate = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chckhere");
        if (chckupdate != null)
        {
            if (chckupdate.Checked)
            {
                strid = GridView1.Rows[i].Cells[1].Text;
                idCollection.Add(strid);
            }
        }
    }
    if (idCollection.Count > 0)
    {
        //call method to update records
        updaterecords(idCollection);
        //rebind the gridview
        GridView1.DataBind();
    }
    else
    {
        //lblmsg.Text = "Please select your favourate candidates";
    }
}


 private void updaterecords(StringCollection idCollection)
 {
//  create sqlconnection and sqlcommand
    SqlConnection conn=new SqlConnection(strconnection);
    SqlCommand cmd=new SqlCommand();
    string ids = "";

    foreach (string id in idCollection)
    {
    ids+=id.ToString()+",";
    }
    try
    {
        string test = ids.Substring(0, ids.LastIndexOf(","));
        string update = "update results set votes=votes+1 where id in (" + test + ")";
        cmd.CommandType = CommandType.Text;

        cmd.CommandText = update;
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    catch (SqlException ex)
    {
        string errormsg = "Error in ballot cast";
        errormsg += ex.Message;
        //throw new Exception(errormsg);
    }
    finally
    {
        conn.Close();
        Response.Redirect("~/Castvotes.aspx");
    }
}

推荐答案

这里的作用是什么?像这样.
What is here @ sign for? Make it like this.
"update results set votes=votes+1 where id in (" + test + ")"


此外,您的问题尚不清楚.您是什么意思-我的主键显示为外键?尝试重新表述您的问题.


Moreover, your question is not clear. What do you mean by - my primary key appear as foreign key? Try to rephrase your question.


这篇关于请帮助!我想使用复选框值进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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