I am doing a function to search for records between 2 date on VB.NET. But I'm not sure is the conversion error or what the result is not showing. I store the date and time in database with DATETIME datatype, and I compare it with the datetimepicker of VB.NET. I guess it is because the datetime store in database include the date and the datatimepicker do not have time in it so the comparison to search the records will never be appear since the comparison is wrong because the datetimepicker do not have time in it.
Here's the code. The dateSelectFrom.value and dateSelectTo.value is the value of the datetimepicker
If rbFirstDate.Checked Then
Dim ds1 As String = Format(dateSelectFrom.Value, "YYYY-MM-DD")
cmd.CommandText = "SELECT OrderList.Id, OrderList.timeOrder,OrderDetail.foodID, Food.foodName,OrderDetail.qty, Food.price, OrderDetail.subtotal FROM OrderDetail INNER JOIN Food on OrderDetail.foodID = Food.Id INNER JOIN OrderList on OrderDetail.orderID = OrderList.Id WHERE (timeOrder = CONVERT(DATETIME, '" & ds1 & "', 102)) order by timeOrder"
End If
If rbBetweenDate.Checked Then
Dim ds1 As String = Format(dateSelectFrom.Value, "YYYY-MM-DD")
Dim ds2 As String = Format(dateSelectTo.Value, "YYYY-MM-DD")
cmd.CommandText = "SELECT OrderList.Id, OrderList.timeOrder,OrderDetail.foodID, Food.foodName,OrderDetail.qty, Food.price, OrderDetail.subtotal FROM OrderDetail INNER JOIN Food on OrderDetail.foodID = Food.Id INNER JOIN OrderList on OrderDetail.orderID = OrderList.Id WHERE (timeOrder BETWEEN CONVERT(DATETIME, '" & ds1 & "', 102) AND CONVERT(DATETIME, '" & ds2 & "', 102)) order by timeOrder"
End If
I had try to put the time format behind the date but it shows error conversion of datetime from string. Which the code looks like this :
Dim ds1 As String = Format(dateSelectFrom.Value, "YYYY-MM-DD HH:MI:SS")
Am I doing the conversion the wrong way or how do I assign the time to the datetimepicker.value? I'm kinda confused by this datetimepicker and DATETIME datatype.