MySQL Error You have an error in your SQL syntax; check the manual that corresponds to your server version for the right syntax to use near '%'

Last Reply 11 months ago By PSowmiya

Posted 11 months ago

My code is:

This shows me an error  in else part MySQL Query.How to solve it.

I have error on sql syntax.

My error is:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''%'' at line 1

            dbConnection()
            Using cmd As New MySqlCommand
                If String.IsNullOrEmpty((formPOS.productSearchTB.Text.Trim())) Then
                    sql = "SELECT Product_ID, Product_ID_Barcode, Name, Price, Category, Price_Code, Name_of_Supplier, Delivery_Date, Unit, Quantity FROM products_database;"
                ElseIf Not String.IsNullOrEmpty((formPOS.productSearchTB.Text.Trim())) Then
                    sql = "SELECT Product_ID, Product_ID_Barcode, Name, Price, Category, Price_Code, Name_of_Supplier, Delivery_Date, Unit, Quantity FROM products_database  Where Name LIKE @p1'%';"
                    cmd.Parameters.AddWithValue("@p1", formPOS.productSearchTB.Text.Trim())
                End If
                cmd.CommandText = sql
                cmd.Connection = conn
                dt = New DataTable
                da = New MySqlDataAdapter
                Dim bsource As New BindingSource
                da.SelectCommand = cmd
                da.Fill(dt)
                bsource.DataSource = dt
                formPOS.PriceListDGV.DataSource = dt
            End Using

Please help

Posted 11 months ago

Sir i got a solution sir.

Thank you so much for taking more efforts

Code is: 

        search = productSearchTB.Text + "%"
        Dim constr As String = "server=localhost;user id=root;password=;database=newpost_database;Convert Zero Datetime=True"
        Using con As MySqlConnection = New MySqlConnection(constr)
            Dim quer As String = "SELECT Product_ID, Product_ID_Barcode, Name, Price, Category, Price_Code, Name_of_Supplier, Delivery_Date, Unit, Quantity FROM products_database Where Name LIKE '" & search & "';"
            Using cmd As MySqlCommand = New MySqlCommand(quer, con)
                Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
                Dim dt As DataTable = New DataTable()
                da.Fill(dt)
                Me.PriceListDGV.DataSource = dt
            End Using
        End Using