Israel

Israel

  • NA
  • 1.3k
  • 204.6k

Repeated sqlparameter's values not allowed. How to resolv?

Jan 10 2020 8:41 AM
Hi,
 
How can I write correctly codes that values are repeated in the segond piece of codes? Why I am asking this question? Just because there are some parameters's values underlined (showing that some values are repeated). But its the real column's name.
 
For example these two pieces codes are into the same button:
 
First piece:
  1. conn = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\x\\WindowsFormsApplication1\\App_datas\\test.mdf;Integrated Security = True;Integrated Security = True");  
  2. comm = new SqlCommand();  
  3. if (conn.State != ConnectionState.Open)  
  4. conn.Open();  
  5. SqlCommand comm = new SqlCommand();  
  6. comm1.Connection = conn;  
  7. SqlParameter date = new SqlParameter("@date", SqlDbType.VarChar);  
  8. SqlParameter name = new SqlParameter("@name", SqlDbType.VarChar);  
  9. comm1.Parameters.Add(date);  
  10. comm1.Parameters.Add(name);  
  11. date.Value = dtDate.Text;  
  12. name.Value = txtName.Text;  
  13. comm.Connection = conn;  
  14. comm.CommandText = "insert into accounting ([date],[name])values(@date,@name)";  
  15. {  
  16. if (MessageBox.Show("Are you sure to save?""", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  17. {  
  18. try  
  19. {  
  20. comm.ExecuteNonQuery();  
  21. }  
  22. finally  
  23. {  
  24. conn.Close();  
  25. }  
Second piece:
  1. // Its underline where its colored in red  
  2. conn = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\x\\WindowsFormsApplication1\\App_datas\\test.mdf;Integrated Security = True;Integrated Security = True");  
  3. comm = new SqlCommand();  
  4. if (conn.State != ConnectionState.Open)  
  5. conn.Open();  
  6. SqlCommand comm = new SqlCommand();  
  7. comm1.Connection = conn;  
  8. SqlParameter date = new SqlParameter("@date", SqlDbType.VarChar); //here  
  9. SqlParameter name = new SqlParameter("@name", SqlDbType.VarChar); // here  
  10. comm1.Parameters.Add(date);  
  11. comm1.Parameters.Add(name);  
  12. date.Value = dtDate.Text;  
  13. name.Value = txtName.Text;  
  14. comm.Connection = conn;  
  15. comm.CommandText = "insert into accounting ([date],[name])values(@date,@name)";  
  16. {  
  17. if (MessageBox.Show("Are you sure to save?""", MessageBoxButtons.YesNo) == DialogResult.Yes)  
  18. {  
  19. try  
  20. {  
  21. comm.ExecuteNonQuery();  
  22. }  
  23. finally  
  24. {  
  25. conn.Close();  
  26. }  

Answers (3)