Livaren Livaren - 4 years ago 133
Vb.net Question

Combobox not populating with mysql data

I have a combobox which needs to be populated with data from mysql table, here is my code, I cannot see what's wrong with it? It doesn't throw any error, just comes blank when I run the program.

Dim StrSql As String = "SELECT PaymentID FROM payment_details"
Dim cmd As New MySqlCommand(StrSql, objconnection)
Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable("Payment_details")

da.Fill(dt)

If dt.Rows.Count > 0 Then
cbxPaymentID.DisplayMember = "PaymentID" 'What is displayed
cbxPaymentID.ValueMember = "PaymentID"
cbxPaymentID.DataSource = dt
End If

Answer Source

The ComboBox has Items. What you need to do is create an new Item for each row in your datatable.

something like....

If dt.Rows.Count > 0 Then
    For r = 0 to dt.Row.Count - 1
        cbxPaymentID.Items.Add(new ListItem(dt.Row(r).Item("PaymentID"))
    Next
End If

The above is based on standard ASP toolkit ComboBox. Other versions may need different code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download