Function skips steps and reruns itself

67 Views Asked by At

I basically have a problem with function re-running itself. Here's what happens:

I have a button on my form This is the click event handler:

Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
    Dim checkResult As Boolean = CheckIfSameDOExists(Me.txbNewDO.Text)
    If (checkResult AndAlso duplicateDOResult = Windows.Forms.DialogResult.Yes) Then
        MessageBox.Show("Duplicate number detected.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        Exit Sub
    ElseIf (Not checkResult AndAlso duplicateDOResult = Windows.Forms.DialogResult.No) Then
        MessageBox.Show("New record was not added!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    Else
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End If
End Sub

And the function in question, CheckIfSameDOExists:

 Private Function CheckIfSameDOExists(ByVal doNum As String) As Boolean
        Dim Result As Boolean = False
        duplicateDOResult = Windows.Forms.DialogResult.Ignore //this is global DialogResult variable
        If (msqlConn.State = Data.ConnectionState.Open) Then
            Dim dr As Data.SqlClient.SqlDataReader = Nothing
            Try
                Using cmd As New Data.SqlClient.SqlCommand(CheckNewDO, msqlConn)
                    cmd.CommandType = Data.CommandType.Text
                    cmd.Parameters.AddWithValue("@doNum", doNum)
                    dr = cmd.ExecuteReader
                    If (dr.Read) Then
                        duplicateDOResult = MessageBox.Show("Record already exists, try again?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                        If (duplicateDOResult = Windows.Forms.DialogResult.Yes) Then
                            Result = True
                        Else
                            Result = False
                        End If
                    End If
                    dr.Close()
                    dr = Nothing
                End Using
            Catch ex As Exception
                If (dr IsNot Nothing AndAlso Not dr.IsClosed) Then
                    dr.Close()
                End If
                dr = Nothing
                MessageBox.Show("Error connecting to DB - can't check records", "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
            End Try
        End If
        Return Result
    End Function

What happens when I do step by step debug in VS, I see the code getting to the duplicateDOResult = MessageBox.Show("Record already exists, try again?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) line and then the function CheckIfSameDOExists starts running again, never reaching the further code. I can't quite see why would that be.

0

There are 0 best solutions below