-2
string query = "Select * from tbl_Items where PurchaseDate=" + dateTimePicker1.Value;
dataGridView1.DataSource = DataAccessLayer.ExecuteTable(query);

Above code is generating error that Incorrect syntax near '6'.. I have tried many time but not get result.if I gave it static date like that ('2019-01-01' instead of datetimepicker value) it show the result correct.so Please anyone have an idea then please.

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20
  • 1
    Do not glue data into strings to make SQL - use Parameters and the problem will go away – Ňɏssa Pøngjǣrdenlarp Mar 16 '20 at 15:59
  • 1
    Does this answer your question? [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements) – Dour High Arch Mar 16 '20 at 16:01
  • 1
    You are missing string delimiter on date provided by DateTimePicker; you must also look at the query string using debug to look at the final query string you send to DataAccessLayer class. – Mauro Destro Mar 16 '20 at 16:20

1 Answers1

-1

You miss an apostrophe or sql delimiter during building Sql code, try the following code :

string query = "Select * from tbl_Items where PurchaseDate='" + dateTimePicker1.Value + "'";

i hope this will help you fix the issue

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20