0

我相信你们一切都好。我想知道我做错了什么以及如何解决它。我对下面代码的意图是查询我的 MySQL 数据库并在 ComboBox 内显示表的一列。Then, when that value in the ComboBox is selected, I want all the records associated to be populated into other controls on my form (I'll create a separate question for this part).

现在,查询正在运行,但 ComboBox 没有被填充。我究竟做错了什么?请帮忙,谢谢。

这是我的代码:

    Private Sub RetrieveMySQLdata()

    Try
        Dim dbConn As New MySqlConnection
        Dim dbQuery As String = ""
        Dim dbCmd As New MySqlCommand
        Dim dbAdapter As New MySqlDataAdapter
        Dim dbTable As New DataTable
        If dbConn.State = ConnectionState.Closed Then
            dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
            dbConn.Open()
        End If

        dbQuery = "SELECT *" & _
                   "FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
                   "WHERE customer.accountNumber = '" & TextBoxAccount.Text & "'"
        With dbCmd
            .CommandText = dbQuery
            .Connection = dbConn
        End With
        With dbAdapter
            .SelectCommand = dbCmd
            .Fill(dbtable)
        End With
        Dim i As Integer
        For i = 0 To dbTable.Rows.Count - 1
            ComboBoxCard.ValueMember = "ccNumber"

        Next
    Catch ex As Exception
        MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
                    vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
    End Try

End Sub
4

2 回答 2

2

你想在哪里填充ComboBox?我看到的唯一交互是在这里:

For i = 0 To dbTable.Rows.Count - 1
    ComboBoxCard.ValueMember = "ccNumber"
Next

我猜这并没有做你认为它正在做的事情。一方面,您ValueMember在一个循环中多次设置为相同的值。语句中的任何内容都不会随着循环的每次迭代而改变,那么为什么要循环呢?

更具体地说,ValueMember实际上不是任何类型的显示值。 它用于指示绑定数据中的哪个字段应该包含 value。当您为DataSource您缺少的控件提​​供 a 时,将使用此选项。

我假设DataSource应该是dbTable,所以你可能想要做这样的事情:

ComboBoxCard.DataSource = dbTable
ComboBoxCard.ValueMember = "ccNumber"
ComboBoxCard.DisplayMember = "Some Other Field in the database"

I don't remember if you need to explicitly call .DataBind() on the control after these statements, but the example I linked to doesn't do it so I'll leave it out.

Essentially what you're trying to do in your code is loop through the results and add them to the ComboBox. You don't need to do this. The ComboBox is capable of doing this internally if you just point its DataSource to the set of data being used and tell it which fields it needs to use on that set. This is called data binding.

于 2012-09-30T12:39:22.053 回答
1

看起来您没有DataSource为 ComboBox 对象设置。而不是这段代码:

Dim i As Integer
For i = 0 To dbTable.Rows.Count - 1
    ComboBoxCard.ValueMember = "ccNumber"
Next

使用类似这样的代码:

ComboBoxCard.DataSource = dbTable
ComboBoxCard.ValueMember = "ccNumber"
ComboBoxCard.DisplayMember = "(some other column if you want)"
于 2012-09-30T12:38:52.537 回答