简体   繁体   中英

Visual Basic how to stop an event after MsgBox is shown

I want to make sure that the user has checked one of my 3 checkboxes before my furthur process, thus I created the function below and generate a Msg if they didn't checked anything. the issue is the process still carries on after the msg box. So is there any way to stop this event after this particular msgbox is shown?

Private Sub btnPay_Click(sender As Object, e As EventArgs) Handles btnPay.Click
        If chkbNotes.Checked = True Then
            conn.Open()
            sql = "UPDATE Order SET payment_method_id = 1 WHERE Id = '" + FoodMenu.OrderID + "';"
            cmd = New SqlCommand(sql, conn)
            cmd.ExecuteNonQuery()
        Else
            If chkbCreditCard.Checked = True Then
                conn.Open()
                sql = "UPDATE Order SET payment_method_id = 2 WHERE Id = '" + FoodMenu.OrderID + "';"
                cmd = New SqlCommand(sql, conn)
                cmd.ExecuteNonQuery()
            Else
                If chkbEwallet.Checked = True Then
                    conn.Open()
                    sql = "UPDATE Order SET payment_method_id = 2 WHERE Id = '" + FoodMenu.OrderID + "';"
                    cmd = New SqlCommand(sql, conn)
                    cmd.ExecuteNonQuery()
                Else
                    MsgBox("Please choose 1 Payment Method", MsgBoxStyle.Exclamation, "Error")
                End If
            End If
        End If
        Dim Amount As String
        Amount = InputBox("Enter Paying Amount (EX:100.00)", "Amount")
        If (IsNumeric(Amount)) = True Then
        .
        .
        .

Here's what your code ought to look like:

Private Sub btnPay_Click(sender As Object, e As EventArgs) Handles btnPay.Click
    Dim paymentMethodId As Integer

    If chkbNotes.Checked Then
        paymentMethodId = 1
    ElseIf chkbCreditCard.Checked Then
        paymentMethodId = 2
    ElseIf chkbEwallet.Checked Then
        paymentMethodId = 3
    Else
        MessageBox.Show("Please choose 1 Payment Method", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Return
    End If

    sql = "UPDATE Order SET payment_method_id = @PaymentMethodId WHERE Id = @Id"
    cmd = New SqlCommand(sql, conn)

    cmd.Parameters.Add("@PaymentMethodId", SqlDbType.Int).Value = paymentMethodId
    cmd.Parameters.Add("@Id", SqlDbType.Int).Value = FoodMenu.OrderID

    conn.Open()
    cmd.ExecuteNonQuery()

I have taken the liberty of assuming that the payemtn method ID should be 3 in the third case rather than 2, which you were using in your original code.

I would also recommend changing your ADO.NEt code as it appears that you are reusing connection and command objects. You should just create and destroy them where and when you need them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM