0

Below is the table: item_id is PrimaryKey and AUTO INCREMENT, item_code is UNIQUE

| item_id | item_code | item_name |
-----------------------------------
|    1        A001        Apple   |
|    2        A002        Orange  |
-----------------------------------

Private Sub Model_Insert(fields As String())
    Dim query As String = "INSERT INTO main_item(item_code,item_name,item_desc,item_unitprice,item_barcode,dept_id,cat_id,gst_id,set_item,active)" &
                          "VALUES (@item_code,@item_name,@item_desc,@item_unitprice,@item_barcode,@dept_id,@cat_id,@gst_id,@set_item,@active)" &
                          "ON DUPLICATE KEY UPDATE 
                           item_code=VALUES(@item_code),
                           item_name=VALUES(@item_name),
                           item_desc=VALUES(@item_desc),
                           item_unitprice=VALUES(@item_unitprice),
                           item_barcode=VALUES(@item_barcode),
                           dept_id=VALUES(@dept_id),
                           cat_id=VALUES(@cat_id),
                           gst_id=VALUES(@gst_id),
                           set_item=VALUES(@set_item),
                           active=VALUES(@active)"

    Using cmd = New MySqlCommand(query, conn)
        cmd.Parameters.AddWithValue("@item_code", fields(0).Trim)
        cmd.Parameters.AddWithValue("@item_name", fields(1).Trim())
        cmd.Parameters.AddWithValue("@item_desc", fields(2).Trim())
        cmd.Parameters.AddWithValue("@item_unitprice", fields(3).Trim())
        cmd.Parameters.AddWithValue("@item_barcode", fields(4).Trim())
        cmd.Parameters.AddWithValue("@dept_id", fields(5).Trim())
        cmd.Parameters.AddWithValue("@cat_id", fields(6).Trim())
        cmd.Parameters.AddWithValue("@gst_id", fields(7).Trim())
        cmd.Parameters.AddWithValue("@set_item", fields(8).Trim())
        cmd.Parameters.AddWithValue("@active", fields(9).Trim())

        Dim numInserted = cmd.ExecuteNonQuery()
    End Using
End Sub

Additional information: 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 ''A001'),item_name=VALUES('Kopi'),' at line 2

I get this error when I try to insert a record with same item_code but diffrent item_name, I want to keep the item_code but replace other column with new value.

vbnewbie
  • 206
  • 6
  • 26

0 Answers0