tags:

views:

53

answers:

2

I have following sql which is show previous days records from table.

SELECT * FROM tblMeter WHERE
(Date >= DATEADD(day, DATEDIFF(day, 1, GETDATE()), 0))
AND
(Date < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0))

But how can I collect records with datetimepicker value in vb.net.

Any help will be appreciated.

Thanks

A: 

There's various ways you can do it, I'd prefer to wrap that sql statement up in a stored procedure that had one input parameter called @myDate or something like that, then I'd use code similar to the one in this sample to retrieve the data and do what I need to do with it.

However, one thing I would change with that SQL statement is that I wouldn't do SELECT *, I'd change that to SELECT [nameOfCol1], [nameOfCol2], etc so that even if someone later adds more columns to that table that statement will still return the same data and there's no risk that my code will use the wrong columns.

ho1
+1  A: 
Dim command As New SqlCommand

command.CommandText = "SELECT * FROM tblMeter WHERE (Date >= @startDate) AND (Date <  @endDate)"
command.Parameters.AddWithValue("@startDate", startDateTimePicker.Value)
command.Parameters.AddWithValue("@endDate", endDateTimePicker.Value)

Then all you need to do is execute the command and use the results. If you need help with that, I can explain it further

thorkia
I thought that I will need your help Thorkia. First time in my life I am trying to write software in my life. Started 40 days ago.
Hakan