Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

multiple datagridview data update to database mysql - vb.net

I have created a shopping system with two different interfaces. The admin interface is used to add/update/delete items to the database and the customer interface to purchase products. I have a problem updating the quantity in the admin interface after the user bought the product. The quantity in the admin interface is updated till the value goes negative.

From admin panel - Before transaction

From User panel - Transaction Process

From admin panel - After transaction

Try
    For Each row As DataGridViewRow In formPOS.ProductAddToCartDGV.Rows
        dbconnection()
        sql = "UPDATE products_database SET Product_Quantity = Product_Quantity - @ProductQuantity WHERE Product_Code = @ProductCode;"
        cmd = New MySqlCommand
        With cmd
            .Connection = conn
            .CommandText = sql
            .Parameters.Clear()
            .Parameters.AddWithValue("@ProductCode", row.Cells(0).Value)
            .Parameters.AddWithValue("@ProductQuantity", row.Cells(3).Value)
            result = .ExecuteNonQuery()

            If result = 0 Then
                MsgBox("Error in updating stocks")
            Else
                MsgBox("Successfully update stocks")
            End If
        End With
    Next
Catch ex As Exception
    MsgBox(ex.Message)
Finally
    conn.Close()
End Try

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...