none
Visual Basic: How to search for date using date time picker and datagridview? RRS feed

Answers

  • No there is no need to convert the date if you use the DateTimePicker "Value" property and use parameters. 

    This is in fact all the code you need. But look at that "Date" behind the datetimepicker value otherwise it is always unequal

    Public Class Form1
        Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
            Dim dt As New DataTable
            Using da As New OleDb.OleDbDataAdapter("SELECT * FROM Orders WHERE OrderDate = ?", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test\nwind.mdb")
                da.SelectCommand.Parameters.AddWithValue("?", DateTimePicker1.Value.Date)
                da.Fill(dt)
                DataGridView1.DataSource = dt
            End Using
        End Sub
    End Class


    I've put this on our website because in fact this question is often asked and seldom correct answered.

    http://www.vb-tips.com/DateTimeDateProperty.ASPX


    Success
    Cor



    Friday, July 7, 2017 9:39 AM

All replies

  • Please provide more context.  If you use a parameterized query there is no need for a conversion, simply use the date value as selected in the control.

    Show an example of the code you are trying to use.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, July 6, 2017 4:30 PM
    Moderator
  • Hi creepyspirals,

    Based on your description, do you want to convert the data into some form? what is some form? I find you use SQL reader, do you want to retrieve some data from Sql database into datagridview? Please give more detailed info about your issue here, or you can post some code here, it is very beneficial to other to analyze your issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 7, 2017 8:48 AM
    Moderator
  • No there is no need to convert the date if you use the DateTimePicker "Value" property and use parameters. 

    This is in fact all the code you need. But look at that "Date" behind the datetimepicker value otherwise it is always unequal

    Public Class Form1
        Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
            Dim dt As New DataTable
            Using da As New OleDb.OleDbDataAdapter("SELECT * FROM Orders WHERE OrderDate = ?", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test\nwind.mdb")
                da.SelectCommand.Parameters.AddWithValue("?", DateTimePicker1.Value.Date)
                da.Fill(dt)
                DataGridView1.DataSource = dt
            End Using
        End Sub
    End Class


    I've put this on our website because in fact this question is often asked and seldom correct answered.

    http://www.vb-tips.com/DateTimeDateProperty.ASPX


    Success
    Cor



    Friday, July 7, 2017 9:39 AM
  • Thank you so much.. It's true I'm learning visual basic and it's really hard to find answers to questions :/. Simply because I feel like there are multiple ways to do one simple task..
    Saturday, July 8, 2017 6:51 PM
  • Thank you so much.. It's true I'm learning visual basic and it's really hard to find answers to questions :/. Simply because I feel like there are multiple ways to do one simple task..

    I'm not a database guy and Cor and I very rarely agree on anything, but he's right.

    Think about it: With a DateTimePicker, you're probably only selecting a date -- not a date and a time but the .Value represents a DateTime instance which has both.

    So to compare two of them, they would literally have to be less than one ten-millionth of a second apart:

    https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, July 8, 2017 10:06 PM
  • Hi creepyspirals,

    Please remember to close your thread by marking helpful post, it is very beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 19, 2017 7:05 AM
    Moderator