0

I have a "if" or "case" statement in the a method that I can called. I have "Flag" varriable as public status. Its Crazy its loop/running for twice, so cause have duplicate command, in these case I have duplicate data on the database.

 Public Sub compartment1(ByVal exec As Boolean)
    Try
         If exec = True Then
             Select Case FlagMark
                Case 1

                     Insert database execute command

                Case 2
                     another command
            End Select

        ElseIf exec = False Then

        End If
    Catch ex As Exception

    End Try
End Sub

And this is my button command

Private Sub cmd_confirm_Click(sender As Object, e As EventArgs) Handles cmd_confirm.Click  

If pCheck2.Checked = True Then

               FlagMark = 1
               compartment1(True)

End If

End Sub

The Method "Compartment1" is running twice, look like looping, so I have a duplicate data or duplicate for single command. Can it running only once?

MFBM
  • 145
  • 1
  • 1
  • 7
  • 1
    Is `FlagMark` `Shared`? It should not be shared, otherwise all users will overwrite this flag each other. But apart from that it's not clear what's causing your issue. Have you set a breapoint in the method to see if it's called twice? If so, look at the call stack to see where it's coming from. – Tim Schmelter Jul 22 '16 at 08:12
  • sure, "FlagMark" is a string variable with public type – MFBM Jul 22 '16 at 08:13
  • Show us HTML code as well. – LifeOfPi Jul 22 '16 at 08:15
  • @TimSchmelter I have set a breakpoint then it running twice. First, its checked button function then go to 'compartement1' method and then execute. Second after method is execute, its back to command/button and its running 'compartement1' method again, then I have duplicate for single command execute. – MFBM Jul 22 '16 at 08:18
  • Please check on how you add the EventHandler. It might have been triggered twice for some reason. Another solution that you might try is to add a edge flag, which if you have done compartment1 once, turn the flag off, so you won't run that 2x – kurakura88 Jul 22 '16 at 08:19
  • @kurakura88: it's ASP.NET – Tim Schmelter Jul 22 '16 at 08:22
  • @MFBM: maybe you have registered this event multiple times. For example via `AddHandler` syntax and with `Handles cmd_confirm.Click`. – Tim Schmelter Jul 22 '16 at 08:22
  • @TimSchmelter: my bad... – kurakura88 Jul 22 '16 at 08:23
  • To confirm that you have to see your HTML code also check whether at server side code same function added with event as @TimSchmelter suggested – LifeOfPi Jul 22 '16 at 08:24
  • All, thanks your support, If the button is html button that have runat=server given, so is effected?. @kurakura88 I have turn off 'FlagMark' varriable its worked prefectly, but it only work if the "case1" block have one command like insert, but if the "Case 1" block have two command, it only one command first is executed then loop go to button function again (I see with breakpoint step). – MFBM Jul 22 '16 at 08:33
  • I actually ask for a new flag for this purpose, so please don't use the FlagMark. And note that this is just a workaround, not a proper fix. You still need to find/ fix what is causing the double event being fired, which I cannot really tell just by looking at the part of your code. – kurakura88 Jul 22 '16 at 08:43
  • Aside from the current question you don't need `Else If` if exec is not True it must be False. Also comparing exec with True is unnecessary exec is already a Boolean so `If exec Then` would work the same (and spare a comparison FWIW) – Sehnsucht Jul 22 '16 at 09:42
  • Make sure your html doesn't have a onclick also. – the_lotus Jul 22 '16 at 11:49

1 Answers1

0

Solved! after I replace the html button that given runat=server attributes with ASP.Net button. Why is so different, If with ASP.NET button the event is running normaly step by step the if or case statement, While using Html button with runnat=server is look like looping process. Even though has a same properties configuration as bellow :

CausesValidation =True EnableViewState=True, etc

MFBM
  • 145
  • 1
  • 1
  • 7