我基本上遇到了重新运行功能的问题。这是发生的事情:
我的表单上有一个按钮 这是click事件处理程序:
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
有问题的函数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
当我在VS中逐步调试时会发生什么,我看到代码到达duplicateDOResult = MessageBox.Show("Record already exists, try again?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
行,然后函数CheckIfSameDOExists
再次开始运行,从未到达更多代码。我不明白为什么会这样。