VB.NET - VB.net SQLCE Query Date Picker and INNER Join

Asked By Tony Reilly-Cooper on 12-Feb-14 10:19 AM
Afternoon peoples! 

Wonder if someone could cast their smarter eyes over my code below. 
Basically I have a SQLCE query built that will count how many times an entry appears in a table then charts the results. I have managed to do this fine but now I am stuck trying to introduce a date range using DateTimePickers. I have to use the DateTimePickers because they have been used to enter the date in the first place so the format is correct. 

This is the current code I have:
        Chart_SpecialityTotal.Visible = True
        Dim startDate As String = DateTimePicker1.Text
        Dim endDate As String = DateTimePicker2.Text
        Dim DBLocation As String = My.Settings.DBLocation.ToString
        Dim conn As SqlCeConnection = New SqlCeConnection("Data Source=" & DBLocation)
        Dim cmd As SqlCeCommand = New SqlCeCommand("SELECT tblSpeciality.SpecialityName AS SpecialityName, COUNT(*) as Total_Entries FROM tblSpeciality INNER JOIN tblReferrals on tblSpeciality.SpecialityName = tblReferrals.Speciality GROUP BY tblSpeciality.SpecialityName", conn)
        conn.Open()
        Dim myDA As SqlCeDataAdapter = New SqlCeDataAdapter(cmd)
        Dim myDataSet As DataSet = New DataSet
        myDA.Fill(myDataSet)
        'DGV_UrgencyTotals.DataSource = New SqlCeDataAdapter(cmd)
        Chart_SpecialityTotal.ChartAreas(0).AxisX.MajorGrid.Enabled = False
        Chart_SpecialityTotal.ChartAreas(0).AxisY.MajorGrid.Enabled = False
        Chart_SpecialityTotal.DataSource = myDataSet.Tables(0)
        Chart_SpecialityTotal.Series("Totals").XValueMember = "SpecialityName"
        Chart_SpecialityTotal.Series("Totals").YValueMembers = "Total_Entries"
        'Chart_ProviderTotal.ChartAreas("ChartArea1").AxisX.MinorTickMark.Enabled = True
        Chart_SpecialityTotal.ChartAreas("ChartArea1").AxisX.Interval = 1 ' 
        Chart_SpecialityTotal.DataBind()
        Chart_SpecialityTotal.Visible = True
        conn.Close()
        conn.Dispose()

The chart displays fine but is displaying totals from the beginning of time. I want to be able to introduce a date range using the date time pickers.

Hope this makes sense. Appreciate your help guys and girls.
Robbe Morris replied to Tony Reilly-Cooper on 12-Feb-14 03:56 PM
You'd want to implement SqlCeParameter in your SqlCeCommand instance.

http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlcecommand.parameters(v=vs.100).aspx

That said, your datetimepicker.text being a string won't work properly with a DateTime column type in sql server.  You'll need to convert the input string value to a .NET DateTime value instead before setting the SqlCeParameter value.

If your start and end date values in the sql server table have hours, minutes, and seconds, you'll want to set your endDate DateTime value to mm/dd/yyyy 11:59:59.998 to make sure you get everything that happened that day.