2012-10-02 59 views
2

好日子:)vb.NET:文本框TextChange

我有一個文本框的參考號,收款人,辦公室和地址......一個程序我想的是,如果在義務表中存在參考號碼它會自動把收款人,辦公室和地址,如果沒有,你會鍵入收款人的名字,但如果存在Payees表中自動它會把辦公室和地址...

我的問題是,它顯示正確的結果,但一個消息框說「數據讀取器中沒有當前查詢」。我認爲代碼重疊,但我不知道如何解決這個問題。

這裏是我的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 

期待答案...或者是有一個如果要是引用號存在,那麼聲明它會忽略txtPayee的textChange?上帝保佑:)

+0

這是*不錯*錯誤處理。你捕捉任何異常,只顯示消息,然後丟棄堆棧跟蹤和任何內部異常。處理特定的異常通常要好得多,因爲你有一個本地策略來處理異常,並讓其他異常傳遞給一個全局異常處理程序,這個異常處理程序將記錄所有的細節,然後終止你的進程。 –

+0

對不起,我不擅長編程......我真的不明白你在說什麼...... :(幫助!:) – bayan0926

+0

我不同意Damien_The_Unbeliever。如果您按照他的建議處理錯誤,用戶將會丟失所有輸入的數據。 – SSS

回答

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 
+0

謝謝..我會試試這個...... :) – bayan0926