Emi Who

Emi Who

  • NA
  • 14
  • 683

Bulk Update all in Gridview

May 26 2021 5:48 PM
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:
  1. protected void gridview1_RowCommand(object sender, GridViewCommandEventArgs e)  
  2. {  
  3. gridview1 row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);  
  4. string Id = e.CommandArgument.ToString();  
  5. DropDownList Ddllsit1 = gridview1.FooterRow.FindControl("Ddllsit1"as DropDownList;  
  6. TextBox box1 = (TextBox)row.FindControl("box1");  
  7. if (e.CommandName == "Insert")  
  8. {  
  9. if (sqlCon.State == ConnectionState.Closed)  
  10. sqlCon.Open();  
  11. SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon);  
  12. sqlCmd.CommandType = CommandType.StoredProcedure;  
  13. sqlCmd.Parameters.AddWithValue("@Action""INSERT");  
  14. sqlCmd.Parameters.AddWithValue("@Id", Id);  
  15. sqlCmd.Parameters.AddWithValue("@type", Ddllsit1.SelectedItem.ToString());  
  16. sqlCmd.Parameters.AddWithValue("@value", box1.Trim());  
  17. sqlCmd.ExecuteNonQuery();  
  18. sqlCon.Close();  
  19. }  
  20. else if (e.CommandName == "Update")  
  21. {  
  22. if (sqlCon.State == ConnectionState.Closed)  
  23. sqlCon.Open();  
  24. SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon);  
  25. sqlCmd.CommandType = CommandType.StoredProcedure;  
  26. sqlCmd.Parameters.AddWithValue("@Action""UPDATE");  
  27. sqlCmd.Parameters.AddWithValue("@Id", Id);  
  28. sqlCmd.Parameters.AddWithValue("@type", Ddllsit1.SelectedItem.ToString());  
  29. sqlCmd.Parameters.AddWithValue("@value", box1.Trim());  
  30. sqlCmd.ExecuteNonQuery();  
  31. sqlCon.Close();  
  32. lblDeleteMessage.Text = "";  
  33. }  
  34. else if (e.CommandName == "Delete")  
  35. {  
  36. if (sqlCon.State == ConnectionState.Closed)  
  37. sqlCon.Open();  
  38. SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon);  
  39. sqlCmd.CommandType = CommandType.StoredProcedure;  
  40. sqlCmd.Parameters.AddWithValue("@Action""DELETE");  
  41. sqlCmd.Parameters.AddWithValue("@Id", Id);  
  42. sqlCmd.ExecuteNonQuery();  
  43. lblDeleteMessage.Text = "Deleted Successfully";  
  44. }  
  45. else if (e.CommandName == "UpdateAll")  
  46. {  
  47. if (sqlCon.State == ConnectionState.Closed)  
  48. sqlCon.Open();  
  49. foreach (GridViewRow row1 in this.gridview1.Rows)  
  50. {  
  51. TextBox value = row1.FindControl("value"as TextBox;  
  52. DropDownList Ddllsit1 = row1.FindControl("Ddllsit1"as DropDownList;  
  53. SqlCommand sqlCmd = new SqlCommand("Storeproc", sqlCon);  
  54. sqlCmd.CommandType = CommandType.StoredProcedure;  
  55. sqlCmd.Parameters.AddWithValue("@Action""UPDATEALL");  
  56. sqlCmd.Parameters.AddWithValue("@Id", Id);  
  57. sqlCmd.Parameters.AddWithValue("@type", Ddllsit1.SelectedItem.ToString());  
  58. sqlCmd.Parameters.AddWithValue("@value", box1.Trim());  
  59. sqlCmd.ExecuteNonQuery();  
  60. sqlCon.Close();  
  61. }  
  62. }  
  63. }  
Let me know what other information I may provide.

Answers (4)