我希望能够通过SQL Server上的连接字符串拥有两个ComboBox,其中一个是第二个的父级或所有者。这意味着每当我在第一个ComboBox
中选择一个值时,第二个ComboBox
将过滤它的结果,以显示与第一个ComboBox
相关的相应值。如果您在下面看到我的代码,我会收到一条错误消息:
错误1方法' Private Sub cboClient_SelectedIndexChanged(sender As Object,e As System.EventArgs,EnvName As String)'无法处理事件' Public Event SelectedIndexChanged(sender As Object,e As System.EventArgs)'因为他们没有兼容的签名。
目标:
我的代码:
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 :(得分: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