Asked by:
Bulk update in ASP.NET gridview (update all)

Question
-
User-1374101463 posted
Similar questions have been asked over the years, but they don't quite address my exact issue.
I have a gridview with a simple Insert, Update, Delete processes which works great.
I want to now have an 'update all' button outside the gridview that will instead of updating one row at a time in my gridview, updates all rows and their changes.
I want to achieve this without checkboxes. (closest example I've seen, but it complicates my form).
What's happening is, I am updating the values across the grid, I click update all, it looks like it's updated, the values are in the viewstate, but when I check the database table values, there are no changes.How can I implement an update all button?
Code behind:
protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e) { gridview1 row = (GridViewRow)(((Control)e.CommandSource).NamingContainer); string Id = e.CommandArgument.ToString(); DropDownList Ddllsit1 = gridview1.FooterRow.FindControl("Ddllsit1") as DropDownList; TextBox box1 = (TextBox)row.FindControl("box1"); if (e.CommandName == "Insert") { if (sqlCon.State == ConnectionState.Closed) sqlCon.Open(); SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@Action", "INSERT"); sqlCmd.Parameters.AddWithValue("@Id", Id); sqlCmd.Parameters.AddWithValue("@type", Ddllsit1.SelectedItem.ToString()); sqlCmd.Parameters.AddWithValue("@value", box1.Trim()); sqlCmd.ExecuteNonQuery(); sqlCon.Close(); } else if (e.CommandName == "Update") { if (sqlCon.State == ConnectionState.Closed) sqlCon.Open(); SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@Action", "UPDATE"); sqlCmd.Parameters.AddWithValue("@Id", Id); sqlCmd.Parameters.AddWithValue("@type", Ddllsit1.SelectedItem.ToString()); sqlCmd.Parameters.AddWithValue("@value", box1.Trim()); sqlCmd.ExecuteNonQuery(); sqlCon.Close(); lblDeleteMessage.Text = ""; } else if (e.CommandName == "Delete") { if (sqlCon.State == ConnectionState.Closed) sqlCon.Open(); SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@Action", "DELETE"); sqlCmd.Parameters.AddWithValue("@Id", Id); sqlCmd.ExecuteNonQuery(); lblDeleteMessage.Text = "Deleted Successfully"; } } //To update all values on button click. protected void UpdateValues() { if (e.CommandName == "UpdateAll") { if (sqlCon.State == ConnectionState.Closed) sqlCon.Open(); foreach (GridViewRow row1 in this.gridview1.Rows) { TextBox value = row1.FindControl("value") as TextBox; DropDownList Ddllsit1 = row1.FindControl("Ddllsit1") as DropDownList; SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@Action", "UPDATEALL"); sqlCmd.Parameters.AddWithValue("@Id", Id); sqlCmd.Parameters.AddWithValue("@type", Ddllsit1.SelectedItem.ToString()); sqlCmd.Parameters.AddWithValue("@value", box1.Trim()); sqlCmd.ExecuteNonQuery(); } sqlCon.Close(); } } protected void LnkUpdateAll_Click(object sender, EventArgs e) { UpdateValues(); }
Wednesday, May 26, 2021 8:57 PM
All replies
-
User-1545767719 posted
how about the following tutorial?
https://docs.microsoft.com/en-us/previous-versions/aspnet/aa992036(v=vs.100)
Wednesday, May 26, 2021 11:07 PM -
User409696431 posted
In this:
TextBox value = row1.FindControl("value") as TextBox; DropDownList Ddllsit1 = row1.FindControl("Ddllsit1") as DropDownList; SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@Action", "UPDATEALL"); sqlCmd.Parameters.AddWithValue("@Id", Id); sqlCmd.Parameters.AddWithValue("@type", Ddllsit1.SelectedItem.ToString()); sqlCmd.Parameters.AddWithValue("@value", box1.Trim()); sqlCmd.ExecuteNonQuery();
How are you sending the value in the textbox to the database? You are finding the textbox and naming it "value", but you are not sending value.Text.Trim() as a parameter. What is "box1.Trim()"?
Friday, June 4, 2021 8:03 PM