I am trying to run the following simple SQL Query that selects data between two dates. The dates come from the following DateTimePickers: DTP_From
, DTP_To
DateTime startDate = DTP_From.Value.Date;
DateTime endDate = DTP_To.Value.Date;
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "SELECT * From Report_Sales where Date >= '" + startDate + "' AND Date <= '" + endDate + "'";
When the query is executed I get the following error:
datetime conversion failed when converting date and/or time from character string
How can I run the above query error free?