-4

I was able to update my data but when I click no or cancel I still get DATA UPDATED message box. What changes do I make to my code to stop the changes and not get the updated message box?

Private Sub BtnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles BtnUpdate.Click
    Try
        da1.Update(ds1, "Table")

        MessageBox.Show("Do you want to update?", "Table", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

        If (DialogResult.Yes) Then
            MessageBox.Show("Data Updated...!")
            End

        ElseIf (DialogResult.No) Then
            End

        ElseIf DialogResult.Cancel Then
            End

        End If

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
GSerg
  • 76,472
  • 17
  • 159
  • 346
sfaj
  • 1
  • 1
  • 5
  • You are not checking the result of `MessageBox.Show`. Even if you did check the result of `MessageBox.Show`, that would have no effect because you are calling `Update` before asking the question. – GSerg Dec 26 '18 at 21:18
  • In order to adapt @GSerg's duplicate, simply replace `MsgBox()` with `MessageBox.Show()` and `MsgBoxResult` with `DialogResult` (`MsgBox` is an outdated VB6 function, so it shouldn't be used these days). – Visual Vincent Dec 26 '18 at 21:25
  • I moved the update command in if statement still it's still showing the msg box data updated. New to coding please guide me how do I rearrange my code to fix the issue. – sfaj Dec 26 '18 at 21:27
  • Fix the first issue he mentioned, see the duplicate. – Visual Vincent Dec 26 '18 at 21:29

2 Answers2

2

You must take the result of your MessageBox.Show, so you must define a variable to store your result.

Try this:

Dim yourResult As DialogResult = MessageBox.Show("Do you want to update?", "Table", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

If (yourResult = DialogResult.Yes) Then
        MessageBox.Show("Data Updated...!")

    ElseIf (yourResult = DialogResult.No) Then Console.WriteLine("Press No")

    ElseIf (yourResult = DialogResult.Cancel) Then Console.WriteLine("Press Cancel")

End If
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
1

You should re-structure your code like this:

Try
    Dim result As DialogResult = MessageBox.Show("Do you want to update?", "Table", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

    If result = DialogResult.Yes Then
        da1.Update(ds1, "Table")
         MessageBox.Show("Data Updated...!")
    ElseIf result = DialogResult.No Then

    ElseIf result = DialogResult.Cancel Then

    End If
Catch ex As Exception
    MessageBox.Show(ex.ToString)
End Try

You placed the da1.Update(ds1, "Table") before the If...Else statements block so that statement gets executed first before the conditions are checked.

preciousbetine
  • 2,959
  • 3
  • 13
  • 29
  • re- structured as directed. this is closing off the Form/application instead of returning to the form. – sfaj Dec 26 '18 at 21:39
  • 3
    @sfaj : That's because you're calling `End` everywhere. Doing so will force close the application. See the docs for more information: [End Statement | Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/end-statement). – Visual Vincent Dec 26 '18 at 21:41
  • @sfaj I have edited the post, remove the `End` Statements – preciousbetine Dec 26 '18 at 21:43
  • Thanks it worked. My bad!! initially I had End statement commented out :) – sfaj Dec 26 '18 at 21:49