How to populate a ComboBox based on another ComboBox using a connection string (SQL Server, VB.NET)

时光毁灭记忆、已成空白 提交于 2019-12-20 07:43:25

问题


When a user selects a client from combobox 1 (a company we do work with and are partners with), its supposed to populate the users from combobox 2 for that client only, which needs to come from that particular database in SQL Server.

Example:

The first Combobox with all the Clients when a user selects a client and the Second ComboBox with all the Users from that database only, I want the Users list of the Second ComboBox to change according to the Clients selected from the list of the first ComboBox, but including a connection string, via SQL Server.

So if I select say....Google in Combobox 1 and in Combobox 2, I expect say.....10 users from Google. But if I change my mind and select Yahoo in Combobox 1 and in Combobox 2, I expect say..... this time around 12 users from Yahoo.

Database Name I'm using: MyDatabase

Get the users from these databases, based on the selection I make, which contains those particular users, not MyDatabase which has users as well:

  • GoogleQA (If user selected Google, get the users from this database only and populate it during runtime)
  • YahooQA (If user selected Yahoo, get the users from this database only and populate it during runtime)

My VB.Net code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr").ConnectionString)
        Dim cmd As New SqlCommand("Select * from CLIENTS", con)
        con.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader())
        con.Close()
        cboClient.DataSource = dt
        cboClient.DisplayMember = "CLIENT_NAME"
        cboClient.ValueMember = "ID"

        rtfMessage.Tag = "Enter your message here"
        rtfMessage.Text = CStr(rtfMessage.Tag)

What can I put in my comboboxes code to get the end goal of what I want to happen:

  Private Sub cboUser_SelectedValueChanged(sender As Object, e As EventArgs) Handles cboUser.SelectedValueChanged
--What do I put here?
    End Sub

Private Sub cboClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClient.SelectedIndexChanged
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("conStr2").ConnectionString)
        Dim cmd As New SqlCommand("Select * from USERS", con)
        con.Open()
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader())
        con.Close()
        cboClient.DataSource = dt
        cboClient.DisplayMember = "NetworkID"
        cboClient.ValueMember = "ID"
        cboClient.Text = ""
        If cboClient.SelectedItem.Text = "Sunoco" Then
            cboUser.Items.Add("NetworkID")
        End If
    End Sub

Possibility: On client change combo box function (cboClient_SelectedValueChanged)

1.) Change the database connection
2.) Query the proper user data from the correct database
3.) populate the user combo box with that user data

回答1:


You just do what you have done in the load section in an if statement inside your user section.

sort of like :

    `If cboClient.SelectedItem.Text = "Google" then
     (code to populate the other combobox with google info)
     End if`


     `If cboClient.SelectedItem.Text = "Yahoo" then 
        dim sql as string = "Select firstName+ " " +lastname as empName from 
        Yahoo table"
        dim cmd as sqladapter(sql, con)
        dim dt as datatable
        cmd.fill(dt)
        cboUsers.datasource = dt
        cboUsers.Datatextfield = "empName"
        cboUsers.Datavaluefield = "empName"
        cboUsers.DataBind()
     End if


        Dim sql2 As String = "Select EQPnumber, EQPdesc from CREW_LINEUP_ACTIVE_EQUIPMENT"
        Dim activeEQPadt As New SqlDataAdapter(sql2, IPMS.Settings.conn)
        activeEQPadt.Fill(activeDT)
        ActiveEQPLstBx.DataSource = activeDT
        ActiveEQPLstBx.DataTextField = "EQPdesc"
        ActiveEQPLstBx.DataValueField = "EQPnumber"
        ActiveEQPLstBx.DataBind()


来源:https://stackoverflow.com/questions/30873264/how-to-populate-a-combobox-based-on-another-combobox-using-a-connection-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!