2015-06-18 93 views
0

我希望能夠通過SQL Server上的連接字符串擁有兩個ComboBoxes,其中一個是第二個父級或所有者。這意味着無論何時我在第一個ComboBox中選擇一個值,第二個ComboBox都會過濾它的結果以顯示與第一個ComboBox相關的對應值。如果你看到下面我的代碼,我得到一條錯誤:如何通過SQL Server基於另一個ComboBox填充ComboBox?

錯誤1方法「私人小組cboClient_SelectedIndexChanged(發送者爲對象,E作爲System.EventArgs,ENVNAME作爲字符串)」無法處理事件「公共事件的SelectedIndexChanged (sender As Object,e As System.EventArgs)',因爲它們沒有兼容的簽名。

目標:

  1. 更改數據庫連接
  2. 查詢從正確的數據庫
  3. 填充用戶組合框與用戶數據正確的用戶數據

我的代碼:

Private Sub cboClient_SelectedIndexChanged(sender As Object, e As EventArgs, ByVal EnvName As String) Handles cboClient.SelectedIndexChanged 
     Using con2 As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString) 
      'Getting connection string from the specific client 
      Dim cmd2 As New SqlCommand("Select * from tblCONNECTIONS where EnvName = '" & "@EnvName" & "'", con2) 
      con2.Open() 
      Dim dt As New DataTable 
      dt.Load(cmd2.ExecuteReader()) 
      cmd2.Parameters.Add("@EnvName", SqlDbType.VarChar) 
      cmd2.Parameters("@EnvName").Value = EnvName 
      If dt.Rows.Count > 0 Then 
       Dim clientConString As String = dt(0)("ConnectionString") 
       'Getting the yalusers info from client specific database\ 
       Using con3 As New SqlConnection(clientConString) 
        cmd2 = New SqlCommand("Select * from yalusers", con3) 
        con3.Open() 
        dt.Load(cmd2.ExecuteReader()) 
        cboUser.DataSource = dt 
        cboUser.DisplayMember = "First_Name" 
        cboUser.ValueMember = "ID" 
        con3.Close() 
       End Using 

      Else 
       'Show error message : missing connection string in the YALCONNECTIONS table 
      End If 

      con2.Close() 
     End Using 
    End Sub 

這怎麼解決?

更新:當我運行該應用程序時,我在第一個組合框中有6個項目,並且我選擇了其中的一個,它不會填充到第二個組合框中。爲什麼??

+0

你應該養成明確命名你的列而不是選擇*無處不在的習慣。 +100雖然爲參數化您的查詢! –

+0

我會再來一次,但這次我需要它。 – Dave

回答

2

您不能將參數添加到事件處理程序。它必須有這樣的定義:

Private Sub cboClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClient.SelectedIndexChanged 
    DoSelectedIndexChanged(sender, e, something) 
End Sub 

創建一個單獨的子程序,完成你需要的工作,但不要讓它處理事件。

Private Sub DoSelectedIndexChanged(sender As Object, e As EventArgs, ByVal EnvName As String) Handles cboClient.SelectedIndexChanged 
+0

對於那個你放在那裏的東西,我想用這個:EnvName。我從一個名爲tblconnection的表中獲得,它的連接字符串列下有一個這樣的URL: Data Source = 111.144.252.84; Initial Catalog = ECRNA; Integrated Security = False; User ID = username; Password =密碼 – Dave

+0

這就是我想要實現的: http://stackoverflow.com/questions/23390646/how-to-set-a-value-to-a-tableadapter-parameter – Dave

相關問題