I want to be able to have two ComboBoxes where one is the parent or owner of the second one, through an connection string on SQL Server. This means that whenever I select a value in the first ComboBox
, the second ComboBox
will filter it's results to display the corresponding values related to the first ComboBox
. If you see my code below, I get an error that states:
Error 1 Method 'Private Sub cboClient_SelectedIndexChanged(sender As Object, e As System.EventArgs, EnvName As String)' cannot handle event 'Public Event SelectedIndexChanged(sender As Object, e As System.EventArgs)' because they do not have a compatible signature.
Goal:
- Change the database connection
- Query the proper user data from the correct database
- populate the user combo box with that user data
My code:
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
How can this be fixed?
Update: When I run the app, I have 6 items in the first combobox and which ever one I select, it doesn't populate into the second combobox. Why??