Israel

Israel

  • NA
  • 1.3k
  • 205.2k

How to Insert between record

Feb 28 2020 8:11 AM
Hi,
 
There is any way to insert a record as it’s possible with edit if you want to edit a table from datagrid. For example I need to insert between row nº 3 and 4 a record. How can do this:
 
ID Date Account
1
2
3
//Insert here
4
5
 
When I need to edit a some rows I do this:
  1. conn = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\xxx\\xx\\App_Datas\\test.mdf;Integrated Security = True");  
  2. comm = new SqlCommand();  
  3. if (conn.State != ConnectionState.Open)  
  4. conn.Open();  
  5. SqlCommand comm1 = new SqlCommand();  
  6. comm1.Connection = conn;  
  7. SqlParameter dat = new SqlParameter("@dat", SqlDbType.Date);  
  8. SqlParameter account = new SqlParameter("@account", SqlDbType.VarChar);  
  9. SqlParameter ID = new SqlParameter("@ID", SqlDbType.Int);  
  10. comm1.Parameters.Add(dat);  
  11. comm1.Parameters.Add(account);  
  12. comm1.Parameters.Add(ID);  
  13. data.Value = dtDate.Text;  
  14. conta.Value = txtAccount.Text;  
  15. ID.Value = lblID.Text;  
  16. comm1.Connection = conn;  
  17. comm1.CommandText = "UPDATE test1 SET date = @date, account = @ account, WHERE (ID = @ID)";  
  18. {  
  19. if (MessageBox.Show("Do you want to insert this record?""Insert Record", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  20. {  
  21. try  
  22. {  
  23. comm1.ExecuteNonQuery();  
  24. }  
  25. finally  
  26. {  
  27. dgvTest.CurrentCell = dgvTest.Rows[dgvTest.Rows.Count - 1].Cells[0];  
  28. conn.Close(); }  
  29. }  

Answers (2)