1

database table : tblsale

fields : id [autonumber], name[text], TOTALQTY [text], TOTALPRICE[text]

lvlist column: QTY, NAME, PRICE

this is my code

sql = "UPDATE tblsale set TOTALQTY = TOTALQTY + lvlist.FocusedItem.getTOTALQTY AND TOTALPRICE = TOTALPRICE + lvlist.FocusedItem.SubItems(2).getTOTALPRICE where name = '" & lvlist.FocusedItem.SubItems(1)

Dim acscmd = New OleDb.OleDbCommand(sql, Con)
acsdr = acscmd.ExecuteReader()

The error I received was

[SYNTAX ERROR in string in query expression 'name = 'Focaccia Bread'.]

i'm trying to to update the record in the tblsale from lvlist record, also i want to add the previous QTY from new one, the same goes to PRICE

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
jez
  • 13
  • 4

1 Answers1

1

There are a lot of problems in your command text.

First you need to look at the correct syntax for an UPDATE command. The AND between the two fields is wrong, you need a comma.

Second you can't put the lvList.FocusedItem.... inside the text of your command. In that way it becomes a literal string and doesn't represent the value to be inserted.

Finally, a parameterized query is always the way to go, Access is more difficult to exploit with Sql Injection because it doesn't support batch statements like SQL Server or MySQL, nevertheless, the parameterized approach has other advantages. For example you don't need to quote strings or numbers and avoid to forget to close a quote like you do at the end of your command.

if lvList.FocusedItem IsNot Nothing Then

    Dim qty = Convert.ToDecimal(lvList.FocusedItem.SubItems(0).Text)
    Dim price = Convert.ToDecimal(lvList.FocusedItem.SubItems(2).Text)
    Dim name =  lvlist.FocusedItem.SubItems(1).ToString()

    sql = "UPDATE tblsale set TOTALQTY = TOTALQTY + @qty, " & _
           "TOTALPRICE = TOTALPRICE + @price " & _  
           "where name = @name"

    Dim acscmd = New OleDb.OleDbCommand(sql, Con)
    acscmd.Parameters.AddWithValue("@qty", qty)
    acscmd.Parameters.AddWithValue("@price", price)
    acscmd.Parameters.AddWithValue("@name", name)
    Dim rowsChanged = acscmd.ExecuteNonQuery()
End If

By the way, FocusedItem doesn't mean SelectedItem. A ListView could have a FocusedItem when it is shown on your form but there is no SelectedItem until your user choose a specific element (at that point FocusedItem and SelectedItem are the same)

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Steve
  • 213,761
  • 22
  • 232
  • 286