SqlDataAdapter Fill Error: Incorrect Syntax near keyword LIKE in Windows Application

Last Reply 5 months ago By dharmendr

Posted 5 months ago

Hello Everyone, 

I have this code that I use for search in Datagrid view

when I run this I get this error :

Incorrect syntax near the keyword 'LIKE'. 

in this part  

sda.Fill(dt)

this is the code :

    Private Function Search() As DataTable
        Dim query1 As String = " Select ID, Date , Description, Quantity, Price, Store, Payment , MwSt FROM dbo.Expenses"
        query1 &= "WHERE ID LIKE '%' + @param1+ '%' "
        query1 &= "OR Date LIKE '%' + @param1+ '%' "
        query1 &= "OR Description LIKE '%' +@param1+ '%' "
        query1 &= "OR Quantity LIKE '%' +@param1+ '%' "
        query1 &= "OR Price LIKE '%' +@param1+ '%' "
        query1 &= "OR Store LIKE '%' +@param1+ '%' "
        query1 &= "OR Payment LIKE '%' +@param1+ '%' "
        query1 &= "OR MwSt LIKE '%' +@param1+ '%' "
        query1 &= "OR @param1 ='' "

        Dim con1 As String = "Data Source=asdasSQLEXPRESS;Initial Catalog=Expenses;Trusted_Connection=yes;"
        Using conn As SqlConnection = New SqlConnection(con1)
            Using cmd As SqlCommand = New SqlCommand(query1, conn)
                cmd.Parameters.AddWithValue("@param1", TextBoxSearch.Text.Trim())

                Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable
                    sda.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Function

Thank you for your help

Best Regards

Posted 5 months ago
elvisidrizi1 says:
Dim query1 As String = " Select ID, Date , Description, Quantity, Price, Store, Payment , MwSt FROM dbo.Expenses"

You are missing space between Table name and the Where.

Add space after table name or before Where.

Dim query1 As String = " Select ID, Date , Description, Quantity, Price, Store, Payment , MwSt FROM dbo.Expenses "
query1 &= " WHERE ID LIKE '%' + @param1+ '%' "