Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table in database with colomn Date (datetime format) that has format "4/2/2015 1:12:15 PM". I select data by two datetimepickes with such statement:
SqlDataAdapter ErrorListTableAdapter = new SqlDataAdapter("SELECT * FROM  ErrorList WHERE (Date BETWEEN '" + Convert.ToDateTime(FromDateTimePicker.Value.ToString("d/M/yyyy h:mm:ss tt")) + "' AND ' " + Convert.ToDateTime(ToDateTimePicker.Value.ToString("d/M/yyyy h:mm:ss tt")) + "')", conn);

But there is no result. What is wrong here?
Posted
Updated 5-Feb-15 0:49am
v4
Comments
Thanks7872 5-Feb-15 5:11am    
First of all,forget about datetime picker. It has nothing to do with your problem.

Further, its very simple to resolve such issue. Make sure you have field datatype as DateTime in your database. Pass only datetime from code to database. Now, your task is only to convert the value of selected date in datepicker to DateTime which can easily be find using Google.com
Member 11246570 5-Feb-15 6:49am    
Rohan Leuva, yes you are right, my data type was not datetime. I changed it, but again I got only empty table without any error? Can you show me the mistake?
Thanks7872 5-Feb-15 6:51am    
4/2/2015 is 4th feb or 2nd apr?
Member 11246570 5-Feb-15 6:55am    
4th of February
Thanks7872 5-Feb-15 7:05am    
Try to convert like this

DateTime From_Time= DateTime.ParseExact("4/2/2015 1:12:15 PM", "d/M/yyyy h:m:s tt", CultureInfo.InvariantCulture);

Now, pass this in query. Note:Always use parameterized queries to be safe from sql injection.

1 solution

As Rohan Leuva said - use parameterized queries. Not just to be safe from SQL injection, but because they give easy solutions to problems like this.

For example:
SQL
SqlDataAdapter ErrorListTableAdapter = new SqlDataAdapter();
ErrorListTableAdapter.SelectCommand = 
   new SqlCommand( "SELECT * FROM  ErrorList WHERE (Date BETWEEN @Date1 AND @Date2)", 
   conn);
ErrorListTableAdapter.SelectCommand.Parameters.Add("@Date1", SqlDbType.Date).Value 
    = FromDateTimePicker.Value.Date;
ErrorListTableAdapter.SelectCommand.Parameters.Add("@Date2", SqlDbType.Date).Value 
    = ToDateTimePicker.Value.Date;
 
Share this answer
 
Comments
Member 11246570 5-Feb-15 7:48am    
CHill60, issue was solved! Thank you))
CHill60 5-Feb-15 7:51am    
What was the problem in the end?
Member 11246570 5-Feb-15 9:01am    
It was related to reportViewer data source, thanks again

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