How to search SQL dattime data with dattime picker

jewel 801 Reputation points
2022-06-15T11:54:17.607+00:00

SQL Server database has a table column, datatype -datetime. I want to search the Datagridview via vb.net but it is not working.
I would have benefited if an experienced person had wasted his precious time and cooperated in the solution. Thanks
Thanks in advance to the experienced collaborators.

Private Function getdata() As Loginout

    Dim cmd As New SqlCommand("  

Select User_Name,User_Type,Time,LoginHistory from tbl_LoginHistory ", con)

    Dim sda As New SqlDataAdapter(cmd)  
    cmd.Parameters.Add("@d1", SqlDbType.Date).Value = Format(Me.DateTimePicker1.Value, "yyyy-MM-dd HH:mm:ss.fff")  

    cmd.Parameters.Add("@d2", SqlDbType.Date).Value = Format(Me.DateTimePicker2.Value, "yyyy-MM-dd HH:mm:ss.fff")  
    Dim dscust As New Loginout  
    sda.Fill(dscust, "DataTable1")  
    Return dscust  

End Function
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,579 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 26,746 Reputation points Microsoft Vendor
    2022-06-20T02:13:08.51+00:00

    Hi @jewel ,
    Change the parameterized query statement to the following code.

    Dim cmd As New SqlCommand("Select User_Name,User_Type,Time,LoginHistory from tbl_LoginHistory WHERE Time BETWEEN '@d1' and '@d2'", con)  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful