I try to make a collection in a combobox. I want the elements in the colloection to come from a database (Access). How can I make this collection??
Printable View
I try to make a collection in a combobox. I want the elements in the colloection to come from a database (Access). How can I make this collection??
I found a way to solve this problem, but only with a static array wich means you`ll get a problem the if the database got further elements than you allow in your collection.
So if anyone knows how it solve this with a dynamic array, you`re welcome.
Here is my solution:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim cs As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb"
Dim sql = "select * from tableName"
Dim row As System.Data.DataRow
Dim s
Dim i = 0
Dim arrlist(100)
Dim dbconn As New OleDbConnection(cs)
Dim command As New OleDbCommand(sql, dbconn)
Dim da As New OleDbDataAdapter(command)
Dim ds As New System.Data.DataSet("DataSetName")
Try
dbconn.Open()
Catch
Label1.Text = ""
Label1.Text = "couldn“t connect to database!" & vbCrLf
End Try
da.Fill(ds, "tableName")
Label1.Text = ""
For Each rad In ds.Tables("tableName").Rows
s = rad.Item("fieldname")
arrlist(i) = s
ComboBox1.Items.Add(arrlist(i))
i += 1
Next
dbconn.Close()
End Sub
You could also set the datasource property of the combo to the datasource after it is filled, instead of looping. Make sure to set the DisplayMember property to the field you want to be displayed.