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.
StringCollection idCollection = new StringCollection();
string strid = string .Empty ;
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)
{
updaterecords(idCollection);
GridView1.DataBind();
}
else
{
lblmsg.Text = "Please select your favourate candidates";
}
}
private void updaterecords(StringCollection idCollection)
{
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;
}
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)