views:

45

answers:

6

I have following code and I can filter data on grid but when I pick same date in 2 datepicker it shows nothing. How can I fix the problem. Any help will be appreciated.

con = New SqlCeConnection(constring)
    con.Open()
    cmd = New SqlCeCommand("SELECT * FROM tblMeter WHERE (Date >= @startDate) AND (Date <  @endDate)", con)
    Dim param1, param2 As SqlCeParameter
    param1 = New SqlCeParameter("@startDate", DateTimePicker1.Value)
    param2 = New SqlCeParameter("@endDate", DateTimePicker2.Value)
    cmd.Parameters.Add(param1)
    cmd.Parameters.Add(param2)
    Dim da As New SqlCeDataAdapter(cmd)
    Dim dt As New DataTable
    da.Fill(dt)
    con.Close()
    DataGridView1.DataSource = dt

Thanks

A: 

Change your query to less-than or equal-to your end date.

SELECT * FROM tblMeter WHERE (Date >= @startDate) AND (Date <= @endDate)

George
This may not work as expected. If the starting and ending dates are 1/1/2010 and 1/20/2010, then the query will find all records with a date between 1/1/2010 00:00 and 1/20/2010 00:00. Therefore, you will get records with a date of midnight on 1/20/2010, but not the records from anytime after midnight on the same date.
NYSystemsAnalyst
I tried this already no effect.
Hakan
A: 

It's your logic:

If date is 2001 and you input 2001:

2001 >= 2001 - check
2001 < 2001 - Nope

Wayne Werner
A: 

Try changing the exclusive

(Date < @endDate)

to the inclusive

(Date <= @endDate)

JohnK813
This won't help. It'd still be just a one millisecond range.
Joel Coehoorn
+3  A: 

param2 = New SqlCeParameter("@endDate", DateTimePicker2.Value.AddDays(1))

NYSystemsAnalyst
Yep, the time will be 00:00:00, so you need to add a day. Adding a day makes the time range go from 00:00:00 to 23:59:59.
smoore
Doesn't work again.
Hakan
Can you please edit your question with your updated code? Also, I hate to sound rude, but are you sure you have data that meets your query parameters? If you execute your query in SQL Management Studio: SELECT * FROM tblMeter WHERE (Date >= '20100623') AND (Date < '20100624') Do you get a result? Substitute your own dates in of course.
NYSystemsAnalyst
+1  A: 

That's because @endDate implies the time 00:00, to include the whole day add the time 23:59:59. Or add 1 to @endDate.

Wez
+2  A: 

Remember that Sql Server interprets a date like this: 2010-06-23
as a date like this: 2010-06-23 00:00:00.000.

In other words, even if you use >= and <= to check the range at both ends you're still only giving yourself a one millisecond timespan. Sometimes you get away with that if all the dates in the column have no time component, but it's rarely what you really want. Instead, you need to add one day to the end of your range so that your filter includes the entire day, and not just the first millisecond.

Joel Coehoorn