Hi Guys,

i am building an application in VB.Net with sql server for databases. I have a screen on which the items in a table are populated in a datagridview based on the combo box selection. A button to commplete the sale has a code that must subtract the number of items sold from the quantity column in SQL server table. The Update code is as shown below.

Private Sub Stockupdate()
        Dim cn As New SqlClient.SqlConnection("Data Source=localhost;Initial Catalog=easysales;Integrated Security=True")
        cn.Open()
        Try
            For Each Isold As DataGridViewRow In dataviewSales.Rows

                Dim Barcode As String
                Dim Inveupdate As New SqlClient.SqlCommand

                Barcode = Isold.Cells(0).Value.ToString()

                Inveupdate.CommandText = "UPDATE tblItems SET Quantity = Quantity - 1 WHERE Barcode=?"

                Inveupdate.Parameters.AddWithValue("@Barcode", Isold.Cells(0).Value.ToString())

                Inveupdate.CommandType = CommandType.Text
                Inveupdate.Connection = cn
                Inveupdate.ExecuteNonQuery()

                Inveupdate.Parameters.Clear()
                Inveupdate.Dispose()
                cn.Close()
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

But when I initiate the code, it giving an error message saying "Syntax error near '?'"
I need help on this particular part.

Recommended Answers

All 2 Replies

Your SQL syntax should be as follows:

Inveupdate.CommandText = "UPDATE tblItems SET Quantity = Quantity - 1 WHERE Barcode=@Barcode"

MsSQl, MySql or Oracle alaways receive named parameter you can use "?" i.e. question mark when you use only OleDb database provider.

Thanks Shark, you just did it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.