dimanche 17 juillet 2016

vb.net IF statement nestled in another IF statement always will return true and never run the 'false' code even if the conditions point to false

I have just been testing a new addition to a small project I am working. I have a form with a Data Grid View on it and an option to delete rows of data. I have a piece of code to change a label's text to a column of that row and then the delete button checks if the label has changed and if it has will proceed to execute the SQL statement and delete the record. However, my problem is even if I choose 'No' on the confirmation to delete message box the IF statement will still run the code to delete the record.

Here is the code I am using, and yes I imagine there is a lot of better ways for me to do this, however, I am learning as I develop more applications so any constructive criticism is gratefully accepted.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles     Button1.Click
    Dim con As New OleDb.OleDbConnection
    con.ConnectionString = My.Settings.databaseConnectionString
    If idToDel.Text = "No Reservation Selected" Then
        MsgBox("Please ensure a reservation is selected", MsgBoxStyle.OkOnly)
    Else
        MsgBox("Confirm the deletion of the reservation: " & idToDel.Text, MsgBoxStyle.YesNo)
        If DialogResult.Yes Then
            Try
                Dim comm As String = "DELETE FROM Reservations WHERE [Customer Name]='" & idToDel.Text + "'"
                Dim sqlComm As New OleDb.OleDbCommand(comm, con)
                con.Open()
                sqlComm.ExecuteNonQuery()
                MsgBox("Successfully deleted the reservation: " & idToDel.Text)
                con.Close()
                Me.ReservationsTableAdapter.Update(Me.HotelDataSet.Reservations)
                Me.ReservationsTableAdapter.Fill(Me.HotelDataSet.Reservations)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        ElseIf DialogResult.No Then
            Me.Close()
        End If
    End If

End Sub

Aucun commentaire:

Enregistrer un commentaire