Israel

Israel

  • 701
  • 1.3k
  • 217.2k

A simple loop when editing

Aug 29 2019 5:08 AM

Hi Developer,

I need help from you concerning a simple loop. 
 
1) I have a table named Test with three columns (ColPositive, ColNegative, ColResult)
2) On my "Form" I have three components : txtColPositive, txtColNegative, btnSave and dvgTest 
When I click on my dvgTest (Datagriviewer) its displaying data selected from the datagridviewer (for this operation its solved!).
Then what I need?
On my btnSave I code wrote to update data. But if its update just one row its will not update the right result on the column ColnResult. Its wrong!
Then on the first click its update the first row then will updating automatically the rest of the rows until the last one (looping).
 
Let's show and explain on this table:

ID ColPositive ColNegative  ColResult

1          100                   0                    100
2              0                 15                      85
3              0                   5                      80
4              0                   0                      70
4            50                   0                    120
5            25                   0                    145
 
Explanation:
 
1) on col1Positive, I insert 100 as positive number its will do plus nothing (or "0") its equal to 100
2) on col1Negative, I insert 15 as negative number its will do minus that last 100 its equal to 85 
... And so on. But its should do a loop until the last record.
3) All the operations loop is coded into btnSave
4) My update code after to select the right row are here:
  1. private void btnSave_Click(object sender, EventArgs e)  
  2. {   
  3. conn = new SqlConnection(connstr);  
  4. comm = new SqlCommand();  
  5. conn.Open();  
  6. SqlParameter ColPositive = new SqlParameter("@ColPositive", SqlDbType.VarChar);  
  7. SqlParameter ColNegative = new SqlParameter("@ColNegative", SqlDbType.VarChar);  
  8. SqlParameter ColResult = new SqlParameter("@ColResult", SqlDbType.VarChar);  
  9. SqlParameter ID = new SqlParameter("@ID", SqlDbType.Int);  
  10.    
  11. comm.Parameters.Add(ColPositive);  
  12. comm.Parameters.Add(ColNegative);  
  13. comm.Parameters.Add(ColResult);  
  14. comm.Parameters.Add(ID);  
  15.    
  16. ColPositive.Value = txtPositive.Text;  
  17. ColNegative.Value = txtNegative.Text;  
  18. ColResult.Value = txtResult.Text;  
  19. ID.Value = lblID.Text;  
  20.    
  21. comm.Connection = conn;  
  22. comm.CommandText = "UPDATE Test SET ColPositive = @ColPositive, ColNegative = @ColNegative, ColResult = @ColResult WHERE (ID = @ID)";  
  23. {  
  24. if (MessageBox.Show("Are you sure to edit?""Editing windows", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  25. {  
  26. try  
  27. {  
  28. comm.ExecuteNonQuery();  
  29. }  
  30. finally  
  31. {  
  32. conn.Close();  
  33. }  
  34. }  
  35. }  
  36. }  
 

Answers (2)