How to search SQL dattime data with dattime picker

jewel 1,231 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
Developer technologies | VB
{count} votes

Answer accepted by question author
  1. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.