Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to update multiple rows by using checkbox in datatlist.
and I want to update only on column in database.


I want to update only "status" column in database, in those rows of datalist where checkbox is checked.

What I have tried:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from a1_form where status='Requested for approval'";
cmd.ExecuteNonQuery();

DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

DataList1.DataSource = dt;
DataList1.DataBind();

con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem dl in DataList1.Items)
{
CheckBox cb = (CheckBox)dl.FindControl("CheckBox1");
if (cb != null)
{
if (cb.Checked)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from a1_form where status='Requested for approval'";
cmd.ExecuteNonQuery();

DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);

foreach (DataRow dr in dt.Rows)
{
dr["status"]="Approved";
}

con.Close();

}
}
}

}
}
Posted
Comments
Karthik_Mahalingam 10-Jun-16 4:06am    
does the table 'a1_form' has primary key ?
if so use the update statement to update the status for selected rows.

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