2

再会:)

我有一个带有参考号,收款人,办公室和地址的文本框的程序......我想要的是,如果参考号存在于义务表中,它将自动输入收款人,办公室和地址,如果没有,您将输入收款人的姓名,但如果 Payees 表中自动存在,它将把 Office 和 Address...

我的问题是它显示了正确的结果,但显示了一个消息框,上面写着“数据阅读器中没有当前查询”。我认为代码相互重叠,但我不知道如何解决这个问题。

这是我的 txtRefNo.Text 代码:

Private Sub txtRefNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRefNo.TextChanged
    Try
        modGlobalFunctions.Connection.Close()
        modGlobalFunctions.connectDatabase()

        Reader = modGlobalFunctions.executeQuery("SELECT DISTINCT ref_no, payee from bims_obligations " & _
                                                 "WHERE ref_no = '" & txtRefNo.Text & "'")

        If Reader.HasRows Then
            While Reader.Read
                txtPayee.Text = Reader("payee").ToString()
                txtOffice.Text = Reader("office").ToString()
                txtAddress.Text = Reader("address").ToString()

                txtPayee.Enabled = False
                txtOffice.Enabled = False
                txtAddress.Enabled = False

                certALoadGrid()

            End While

        Else

            txtPayee.Clear()
            txtOffice.Clear()
            txtAddress.Clear()

            txtPayee.Enabled = True
            txtOffice.Enabled = True
            txtAddress.Enabled = True

        End If

        Reader.Close()

        modGlobalFunctions.Connection.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "BIMS", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    modGlobalFunctions.Connection.Close()
End Sub

对于 txtPayee.text:

   Private Sub txtPayee_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPayee.TextChanged
    Try

        modGlobalFunctions.Connection.Close()
        modGlobalFunctions.connectDatabase()

        Reader = modGlobalFunctions.executeQuery("SELECT * from bims_payee " & _
                                                 "WHERE payee = '" & txtPayee.Text & "'")

        If Reader.HasRows Then
            While Reader.Read
                txtOffice.Text = Reader("office").ToString()
                txtAddress.Text = Reader("address").ToString()

            End While

        Else

            txtOffice.Clear()
            txtAddress.Clear()

        End If

        Reader.Close()

        modGlobalFunctions.Connection.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "BIMS", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    modGlobalFunctions.Connection.Close()

End Sub

期待答案......或者是否有一个 if 语句,如果 refNo 存在,那么它将忽略 txtPayee 的 textChange ???上帝保佑 :)

4

1 回答 1

1

我相信这条线:

txtPayee.Text = Reader("payee").ToString()

会引起txtPayee_TextChanged火灾。然后调用:

modGlobalFunctions.Connection.Close()
modGlobalFunctions.connectDatabase()

您没有向我们展示这些函数的代码,但这些名称肯定具有暗示性。然后在内部使用这个全局连接对象txtPayee_TextChanged,并在底部再次关闭它。

最后,里面的代码txtRefNo_TextChanged以这些行继续:

txtOffice.Text = Reader("office").ToString()
txtAddress.Text = Reader("address").ToString()

但这Reader与已关闭两次的连接相关联(或以某种方式再次替换,您没有显示该代码) - 您将遇到错误。


这只是为什么拥有一个全局共享对象是一个坏主意的一个原因(如果/当您想开始使用后台工作程序、任务或任何其他涉及多线程的东西时,这也很糟糕)Connection

在你的模块中只有一个连接字符串会好得多。modGlobalFunctions然后,在每个函数内部,创建单独SqlConnectionSqlCommand对象 - 将每个对象放在Using语句中。这样他们就不会互相干扰。

我猜是

modGlobalFunctions.Connection.Close()

在每个函数的顶部添加以解决同一问题的早期症状


另外,正如我的评论中所指出的那样 - 不要抓住Ex as Exception并只显示Ex.Message. 您不知道可能会抛出什么异常,并且您正在处理许多有用的信息(例如堆栈跟踪和内部异常)


例如,这就是我写你的第一个子的方式:

Private Sub txtRefNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRefNo.TextChanged
    Using conn As New SqlConnection(ConnectionString) 'Our own private connection, no one else can interfere
        Using cmd As New SqlCommand("SELECT DISTINCT ref_no, payee from bims_obligations WHERE ref_no = @RefNo", conn) 'Using parameters, safer SQL
            cmd.Parameters.AddWithValue("@RefNo", txtRefNo.Text)
            conn.Open() 'Open the connection
            Dim Reader = cmd.ExecuteReader()
            If Reader.HasRows Then
                While Reader.Read
                    txtPayee.Text = Reader("payee").ToString()
                    txtOffice.Text = Reader("office").ToString()
                    txtAddress.Text = Reader("address").ToString()

                    txtPayee.Enabled = False
                    txtOffice.Enabled = False
                    txtAddress.Enabled = False

                    certALoadGrid()

                End While

            Else

                txtPayee.Clear()
                txtOffice.Clear()
                txtAddress.Clear()

                txtPayee.Enabled = True
                txtOffice.Enabled = True
                txtAddress.Enabled = True

            End If
            Reader.Close() 'Not really necessary, but anyway
        End Using
    End Using 'These have cleaned up our connection and command objects
        'If an exception has occurred, we don't know what it is, or how to recover
        'So we don't try and catch it.
End Sub
于 2012-10-02T06:17:03.597 回答