Israel

Israel

  • 631
  • 1.3k
  • 217.3k

Incorrect syntxt near '?'

May 18 2018 6:53 AM
Hi,
 
I worte these codes and its works very well when I use MSAccess Database. Curiosly doesnt works usin sql database. When I make filter its gives this message: Incorrect syntax near '?'
 
Please any help:
  1. conn.Open();  
  2. SqlCommand cmd = conn.CreateCommand();  
  3. cmd.CommandType = CommandType.Text;  
  4. //cmd.CommandText = "select * from taxas_habitacionais where (data between ? and ?) and name = ?";  
  5. cmd.CommandText = "select * from livro1 where (data between ? and ?)";  
  6. var param1 = new SqlParameter("@StartDate", SqlDbType.Date);  
  7. var param2 = new SqlParameter("@EndDate", SqlDbType.Date);  
  8. //var param3 = new OleDbParameter("@name", OleDbType.VarChar); // use actual type of 'name' here  
  9. param1.Value = dtInicialLivro1.Value.Date;  
  10. param2.Value = dtFinalLivro1.Value.Date;  
  11. //param3.Value = txtConsFaltasPorNomes.Text; // or whatever  
  12. if (dtFinalLivro1.Value <= dtInicialLivro1.Value)  
  13. {  
  14. cmd.Parameters.Add(param1);  
  15. cmd.Parameters.Add(param2);  
  16. }  
  17. else  
  18. {  
  19. cmd.Parameters.Add(param2);  
  20. cmd.Parameters.Add(param1);  
  21. }  
  22. //cmd.Parameters.Add(param3);  
  23. cmd.ExecuteNonQuery();  
  24. DataTable dt = new DataTable();  
  25. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  26. da.Fill(dt);  
  27. conn.Close();  
  28. dgvLivro1.DataSource = dt;  

Answers (3)