I have a gridview with a simple Insert, Update, Delete processes which works great.
I want to now have a 'save all' feature that will go a step further than the simple update. Instead of updating one row at a time in my gridview, I want a button to update all rows and their changes if any.
What's happening is, I am updating the values across the grid, I click update all, it looks like it's updated, but when I check the database table values, there are no changes. Can anyone help?
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";
- }
- else 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();
- }
- }
- }
Let me know what other information I may provide.