Click here to Skip to main content
15,867,983 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
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.

C#
    //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
SQL
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
SQL
ALTER proc [dbo].[Castvote]
  (@pic_id int,
   @votes int
  )
  as
  update results
  set votes=votes+1
  where pic_id in (@pic_id)
Posted
Updated 11-Aug-11 11:51am
v4
Comments
OriginalGriff 11-Aug-11 3:29am    
And what problem are you having?
Code dumps with some explanation are not very helpful...

1 solution

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,
 
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