使用复选框更新数据库上的增量值. [英] Update an incremental values on database using checkboxes.

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

问题描述

我有这个C#代码,我想使用复选框更新db上的增量值.这段代码不会更新数据库中的值,什么也不做.

I''ve this c# code i want to update an incremental values on db using checkboxes. This code does not update the value in database, just do nothing.

    //create string collection t store ids of record to be deleted
    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 = string.Empty;

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


//需要更新结果的数据库表


//database table for results need to be updated

ALTER proc [dbo].[add_results]
as
insert into results (pic_id ,pic_name ,pic ,banner ,votes)
select pic_id ,pic_name ,pic ,banner ,0
from Candidate_list



//更新程序



//proc for update

ALTER proc [dbo].[Castvote]
  (@pic_id int,
   @votes int
  )
  as
  update results
  set votes=votes+1
  where pic_id in (@pic_id)

推荐答案

我建​​议逐步调试,然后告诉我们您找到了什么.另外,考虑告诉我们哪个位有效,当它无效时会发生什么,等等.这就是我们在做您的工作,这不是难题,您越努力,就越想做免费的工作您,
I suggest stepping through with a debugger, then telling us what you found. Also, consider telling us which bit is working, what happens when it does not work, etc. This is us doing your job, it''s not a puzzle where the harder you make it, the more we want to do free work for you,


这篇关于使用复选框更新数据库上的增量值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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