//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"); } }
"update results set votes=votes+1 where id in (" + test + ")"
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)