Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to select records from data base where date = date in datetimepicker I'm using this but Error occurs "Conversion failed when converting date/time from character string".

datetimepicker format is "long" and in SQL datatype of Date Column is date

SqlDataAdapter com = new SqlDataAdapter("select * from Farmers where Date="+dateTimePicker1.Value.Date+"", con);
DataSet ds = new DataSet("Farmers");
con.Open();
com.Fill(ds);
con.Close();
Posted

Change the DataType of the date column in SQL to datetime and leave the dateTimePicker1.Value to default settings

use:

"SELECT * FROM Table WHERE Date= '" + Convert.ToDateTime(dateTimePicker1.Value)' ";
 
Share this answer
 
Use a parametrised query instead:
C#
SqlDataAdapter com = new SqlDataAdapter("SELECT * FROM Farmers WHERE Date=@DT", con);
com..SelectCommand.Parameters.AddWithValue("@DT", dateTimePicker1.Value.Date);
DataSet ds = new DataSet("Farmers");
con.Open();
com.Fill(ds);
con.Close();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900