美好的一天:)
我有一个带有文本框的程序,用于参考编号,收款人,办公室和地址......我想要的是,如果参考编号存在于义务表中,它会自动放入收款人,办公室和地址,如果没有,你会输入收款人的姓名,但如果自动存在于Payees表中,则会将Office和地址...
我的问题是它显示正确的结果但是一个消息框说“#34;数据阅读器中没有当前查询。”#34;我认为代码相互重叠,但我不知道如何解决这个问题。
这是我的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 ???上帝保佑:)
答案 0 :(得分: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
模块中建立连接 string 会好得多。然后,在每个函数内部创建单独的SqlConnection
和SqlCommand
对象 - 将每个对象放在Using
语句中。这样他们就不会互相干扰。
我猜是
modGlobalFunctions.Connection.Close()
在每个函数的顶部添加以解决同一问题的早期症状
此外,正如我的评论所示 - 请勿抓住Ex as Exception
,只显示Ex.Message
。你不知道可能会抛出什么异常,并且你正在处理许多有用的信息(例如堆栈跟踪和内部异常)
E.g。这就是我写第一个子的方式:
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