Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Dear Experts I want to ask a question

This Query is working fine.
C#
string query = "select txtDate,txtCustomerName,txtAddress from EntryTable where txtCustomerName='" + txtCustomerName.Text + "' AND txtDate >= '" + Convert.ToDateTime(dateTimePicker1.Value) + " ' AND txtDate <= '" + Convert.ToDateTime(dateTimePicker2.Value) + "' ";

But I have some problem when i am not selecting any date from the datetimepicker (1& 2) means equal date (like 17.9.2013 in datetimepicker1 and 17.9.2013 in datetimepicker2) Its not find any data of this date.
if I am selecting in datetimekeper1 “16.9.2013 “ then all the data displayed.

Please tell me what is the condition will be put in the query.
Posted
v3

1 solution

Your date is all set for this one day, that's true, but it contains more than the date, it also contains the time! so comparing using both will result only in those entries which were inserted in the exact time point which probably returns nothing...

If you want all entries of the day, in your example it is 17.9.2013, make sure the dateTimePicker1.Value after converted, has the day's very first time value of 00:00:00
and the dateTimePicker2.Value, after converted, has the largest time value of 23:59:59.997

before you use the datetime in your query set the TIME,
Example:
C#
// use dt1 instead of the dateTimePicker1.Value (and the same for dt2)
Datetime dt1 = dateTimePicker1.Value.Date.AddHours(0).AddMinutes(0).AddSeconds(0);
Datetime dt2 = dateTimePicker2.Value.Date.AddHours(23).AddMinutes(59).AddSeconds(59);



Cheers,
Edo
 
Share this answer
 
v6
Comments
indiancodinglove 17-Sep-13 7:52am    
now i am use this query its give me right result is it ok?

string dt1 = dateTimePicker1.Value.ToString("yyyy.MM.dd");
string dt2 = dateTimePicker2.Value.ToString("yyyy.MM.dd");
string query = "select txtDate,txtCustomerName,txtAddress from EntryTable where Convert(varchar(10),txtDate,102) between '" + dt1 + "' and '" + dt2 + "'";
Joezer BH 17-Sep-13 8:09am    
No, you still need to set the TIME, not only the date
indiancodinglove 17-Sep-13 8:18am    
Sir Please tell me how to set the time.
Joezer BH 17-Sep-13 8:33am    
See updated solution with editing the Time values of a Datetime struct
indiancodinglove 17-Sep-13 8:51am    
It Gives a error

"Property or indexer 'System.DateTime.Hour' cannot be assigned to -- it is read only"

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