Israel

Israel

  • 705
  • 1.3k
  • 216.3k

Running a simple loop for displaying and saving once

Feb 11 2020 4:07 AM
Hi,
 
I have one table and two tools:
 
“TblCompany”
“txtAccountNumber”
“btnRun”
 
My table looks like this:
 
ID | Account | Name
1 5.6 Selling
2 3.5 Bying
3 1.4 Mobile
 
I wrote some codes that make a normal and simple filter. The target filter is one the column “Account”.
 
Then why I’am doing to resolv it? I put each ID number inside of my codes and copy/paste the same block of codes to filter the next ID number to make the operation. But there is a problem? This table is growing up every day and its becoming little bit havy when processing. Then how can I do to make a one way loop until the latest record of my table displaying one by one the next account’s number that will be saved later and so on.
 
Please see my codes:
  1. string connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";  
  2. SqlConnection conn = new SqlConnection(connectionString);  
  3. conn.Open();  
  4. string commandString = "select top 1 * from tblTest1 where ID='" + txtID.Text + "' order by ID DESC";  
  5. SqlCommand sqlCmd = new SqlCommand(commandString, conn);  
  6. SqlDataReader read = sqlCmd.ExecuteReader();  
  7. if (read.HasRows)  
  8. {  
  9. while (read.Read())  
  10. {  
  11. txtAccount.Text = read["Account"].ToString();  
  12. txtName.Text = read["Name"].ToString();  
  13. }  
  14. }  
  15. else  
  16. {  
  17. }  
  18. read.Close();  
  19. conn.Close();  
  20. conn = new SqlConnection"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";);  
  21. if (conn.State != ConnectionState.Open)  
  22. conn.Open();  
  23. SqlCommand comm = new SqlCommand();  
  24. comm.Connection = conn;  
  25. SqlParameter account = new SqlParameter("@account", SqlDbType.Varchar);  
  26. SqlParameter name = new SqlParameter("@name", SqlDbType.Varchar);  
  27. comm.Parameters.Add(account);  
  28. comm.Parameters.Add(name);  
  29. account.Value = txtAccount.Text;  
  30. name.Value = txtName.Text;  
  31. comm.Connection = conn;  
  32. comm.CommandText = "insert into tblTest2 ([account],[name])values(@account,@name)";  
  33. {  
  34. {  
  35. try  
  36. {  
  37. comm.ExecuteNonQuery();  
  38. }  
  39. finally  
  40. {  
  41. conn.Close();  
  42. }  

Answers (2)