0

I'm using datetimepicker to choose the date to show the data from the microsoft sql server but the cells are empty.

enter image description here

I use LIKE operator to show the purchased item within the day.

This is the code:

 Public Sub see4()
        'MYPOPUL4TE D4T4GRIDVIEW3
        Dim query As String = "SELECT * FROM projectsd.dbo.cart WHERE dates LIKE '" & DateTimePicker1.Text & "'"
        Using cmd As SqlCommand = New SqlCommand(query, con)
            Using da As New SqlDataAdapter
                da.SelectCommand = cmd
                Using dt As New DataTable()
                    da.Fill(dt)
                    DataGridView3.DataSource = dt
                End Using
            End Using
        End Using
    End Sub
Thom A
  • 88,727
  • 11
  • 45
  • 75
fool
  • 1
  • 1
  • Using a `LIKE` against a date and time column doesn't make any sense here, and injecting your value is a major security flaw; ***PARAMETRISE*** your code. Looks like you have 2 design flaws here: 1. Storing date and time values as a `varchar`/`nvarchar`. 2. Injection of "parameters". You need to fix both and likely your query will work fine. – Thom A Mar 24 '21 at 12:07
  • Please refer to this link https://stackoverflow.com/a/1629135/2206420 if you are using LIKE in where condition for date. Otherwise you can parse the datetimepicker.text and use dates=date.parse("") – Kalpesh Bhadra Mar 24 '21 at 12:09
  • 2
    I don't recommend that answer at all, @KalpeshBhadra ; it's not SARGable. – Thom A Mar 24 '21 at 12:09
  • Thank you for answering but what data type should i use?, could you explain how i can inject parameters sorry for the bad english. – fool Mar 24 '21 at 12:10
  • *"Thank you for answering but what data type should i use?"* For a date( and time) value? Any of the [5 date (and time) data types](https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver15#DateandTimeDataTypes) are infinitely more suitable. – Thom A Mar 24 '21 at 12:11
  • thank you i never notice the datetime data types – fool Mar 24 '21 at 12:13
  • I you didn't know about those, I suggest you learn about the data types, @fool . [varchar is NOT a one size fits all data type](https://wp.larnu.uk/fundamentals-varchar-is-not-a-one-size-fits-all-data-type/) – Thom A Mar 24 '21 at 12:24

0 Answers0