Kenzakie20 Kenzakie20 - 4 months ago 34
Vb.net Question

how to deduct a quantity from a database using VB.NET

I used to work with an Inventory System, I Have an error on my code it says "No value given for one or more required parameters." and the error was thrown to

cmd.ExecuteNonQuery()
what does it mean? can someone help me? By the way this code is for deducting Item Quantity on Database. sorry for my bad english.


con.Open()
Dim sqlQry As String = "UPDATE [tbl_Stocks]
SET [Quantity] = [Quantity] - @QU
WHERE Products='" & lbPro.Text & "'"
Using cmd As New OleDbCommand(sqlQry, con)
cmd.Parameters.AddWithValue("@QU", lbQuan.Text)
cmd.ExecuteNonQuery()
con.Close()
MsgBox("Save Successfully!")
End Using

Answer

I don't see any flaw. Try this substitute(a little tweak sending both inputs via parameter)

con.Open()
Dim sqlQry As String = "UPDATE [tbl_Stocks] SET [Quantity] = [Quantity] - @QU " &
                       "WHERE Products=@prod"
Using cmd As New OleDbCommand(sqlQry, con)
    cmd.Parameters.AddWithValue("@QU", lbQuan.Text)
    cmd.Parameters.AddWithValue("@prod", lbPro.Text)
    cmd.ExecuteNonQuery()
    con.Close()
    MsgBox("Save Successfully!")
End Using