[英]vb.NET: Textbox TextChange
美好的一天:)
我有一個帶有用於參考號,收款人,辦公室和地址的文本框的程序...我要的是,如果在義務表中存在參考號,它將自動輸入收款人,辦公室和地址,如果沒有,您將輸入收款人的姓名,但是如果收款人表格中自動存在,則會將辦公室和地址...
我的問題是它顯示正確的結果,但顯示一個消息框,提示“數據讀取器中沒有當前查詢”。 我認為代碼彼此重疊,但是我不知道如何解決這個問題。
這是我的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? 上帝保佑 :)
我相信這一行:
txtPayee.Text = Reader("payee").ToString()
將導致txtPayee_TextChanged
觸發。 然后調用:
modGlobalFunctions.Connection.Close()
modGlobalFunctions.connectDatabase()
您尚未向我們展示這些功能的代碼,但這些名稱肯定具有暗示性。 然后,您可以在txtPayee_TextChanged
使用此全局連接對象,並在底部再次將其關閉。
最后, txtRefNo_TextChanged
內部的代碼恢復為以下txtRefNo_TextChanged
行:
txtOffice.Text = Reader("office").ToString()
txtAddress.Text = Reader("address").ToString()
但是該Reader
與一個已關閉兩次(或以某種方式替換,但又沒有顯示該代碼)的連接相關聯-您將遇到錯誤。
這僅僅是一個原因,為什么有一個全局共享的Connection
對象是一個壞主意(這也是壞的,如果/當你要開始使用后台工作人員,任務,或其他任何東西,包括多線程)。
最好在modGlobalFunctions
模塊中有一個連接字符串 。 然后,在每個函數內部,創建單獨的SqlConnection
和SqlCommand
對象-將每個對象放在Using
語句中。 這樣他們就不會互相干擾。
我猜是
modGlobalFunctions.Connection.Close()
在每個功能的頂部添加了治愈同一問題的早期症狀
另外,如我的評論中所述-不要將Ex as Exception
捕獲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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.