Question

I have this sample format of date in my db 2/26/2014 10:14:04 AM

How can I search for it using a DateTimePicker?

I hope someone would help me. I'm still a newbie.

sql = "SELECT * FROM tbltransact WHERE date =" & dtpTD.Value.Date & "" 
With cmd 
    .Connection = con 
    .CommandText = sql 
End With 
    da.SelectCommand = cmd 
    da.Fill(dt) 
    DataGridView1.DataSource = dt
Was it helpful?

Solution 2

You could try making your query this way instead:

Dim myDate As DateTime = dtpTD.Value.Date
sql = String.Format("SELECT * FROM tbltransact WHERE YEAR(date) = {0} AND MONTH(date) = {1} AND DAY(date) = {2}", myDate.Year, myDate.Month, myDate.Day)

This will give you all the records in tbltransact table for the selected day, regardless of the time.

UPDATE

Be aware though that is is considered bad practice to compose your SQL statements this way. Even worse to mix your data layer code with your UI layer code. I suggest that you try and learn how ADO.NET works with parameters so that you can more safely pass values up to your queries.

OTHER TIPS

Try using the DatePicker control. This allows you to easily select a date. Here is an example of using the DatePicker control

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top