I can not decrease the quantity of stock

-1

I am trying to reduce the quantity of the stock at the time of the sale and without success, I used the code below, no error appears ..

Public Sub UpdateDecreaseQuantity()
    Try
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim OtherValue As Integer = 0
            Dim cb As String = "Update Dish SET Quantity = Quantity - @OtherValue"
            con.Open()
            cmd = New OleDbCommand(cb, con)
            Integer.TryParse(row.Cells(2).Value, OtherValue)
            cmd.Parameters.Add("@OtherValue", OleDbType.Integer).Value = OtherValue
            cmd.ExecuteNonQuery()
            con.Close()
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        cmd.Dispose()
        con.Close()
    End Try

End Sub
    
asked by anonymous 07.12.2017 / 09:15

2 answers

0

It worked fine using the code below:

con = New OleDbConnection(cs)
        con.Open()
        Dim cb As String = "update Dish set Quantity = Quantity - '" & Val(DataGridView2.CurrentRow.Cells(2).Value) & "' WHERE ItemID = " & Val(DataGridView2.CurrentRow.Cells(0).Value) & ""
        cmd = New OleDbCommand(cb, con)
        cmd.ExecuteNonQuery()
        con.Close()
    
09.12.2017 / 05:46
0

You need to debug the code to find out which line is not doing what it should do.

Reading the code above, there are two points where the problem might be ...

First :

Integer.TryParse(row.Cells(2).Value, OtherValue)

You are not verifying that the conversion was successful. The TryParse method returns a boolean indicating that everything has worked out. You need a If to verify this.

Second :

cmd.Parameters.Add("@OtherValue", OleDbType.Integer).Value = OtherValue

You should check in the documentation if this is what you do to inform a parameter in the query. I've never used OleDb so I do not know if it's right. It's good for you to know.

    
08.12.2017 / 00:34