Do you want to ensure that the value entered by customer is less than or equal to the remaining total? If so, you can add a condition Product_Quantity >= CAST(@ProductQuantity AS int)
in WHERE
clause.
For i As Integer = 0 To CustomerDGV.Rows.Count - 2
Dim constr As String = "connection string"
Dim sql As String = "UPDATE products_database SET Product_Quantity = Product_Quantity - @ProductQuantity WHERE Product_Code = CAST(@ProductCode AS int) AND Product_Quantity >= CAST(@ProductQuantity AS int)"
Using conn As SqlConnection = New SqlConnection(constr)
conn.Open()
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.Parameters.AddWithValue("@ProductCode", CustomerDGV.Rows(i).Cells(0).Value)
cmd.Parameters.AddWithValue("@ProductQuantity", CustomerDGV.Rows(i).Cells(1).Value)
Dim rows As Integer = cmd.ExecuteNonQuery()
If rows = 0 Then
MessageBox.Show($"Make sure {CustomerDGV.Rows(i).Cells(0).Value} ProductQuantity <= Product_Quantity")
End If
End Using
Next
Note: this is a SQL Server demo.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…