【问题标题】:How to populate a ComboBox based on another ComboBox through SQL Server? [duplicate]如何通过 SQL Server 填充基于另一个 ComboBox 的 ComboBox? [复制]
【发布时间】:2015-06-19 03:21:21
【问题描述】:

我希望能够通过 SQL Server 上的连接字符串拥有两个组合框,其中一个是第二个的父级或所有者。这意味着每当我在第一个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)' 因为它们没有兼容的签名。

目标:

  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 个项目,而无论我选择哪一个,它都不会填充到第二个组合框中。为什么??

【问题讨论】:

  • 您应该养成明确命名列的习惯,而不是到处选择 *。 +100 用于参数化查询!!!
  • 我会再做一次,但这次我需要它。

标签: sql-server vb.net combobox


【解决方案1】:

您不能将参数添加到事件处理程序。它必须有这个定义:

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

【讨论】:

  • 对于你放在那里的那个“东西”部分,我想用这个:EnvName。我从一个名为 tblconnection 的表中获取,它对于 connectionstring 列下的值有一个这样的 URL:Data Source=111.144.252.84;Initial Catalog=ECRNA;Integrated Security=False;User ID=username;Password=password
  • 这就是我想要完成的:stackoverflow.com/questions/23390646/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多