How to Bind Data to a ComboBox from MS Access DB to VB Form
Posted on 2006-05-15
Hey whats up experts!!
I have a small problem. I want to bind a combobox with data from my MS access file. All I want to do is get a particular Field name and populate
the combobox with its attributes within the combobox selection. So if I have to pick let say Names I want the combo box to read off the database and collect
all the names within that particular field name and populate itself within the combo box so like that when the user decides to use the combo box they get
to see all the users in it.
I so far Im having trouble getting this to work....I though of using a OledbDataReader to see if that would be possible , but so far I'm unsuccessful at finishing the task.
I was wondering if it is indeed the way to do this to begin with...Here is my sample code and please let me know if I went the wrong way of the road...which im sure I did lol
///////check this out guys/////////
Dim myConnection As New OleDb.OleDbConnection(" Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\db2.mdb; ")
Dim DirectReader As OleDb.OleDbDataReader
Dim SearchCmd As New OleDb.OleDbCommand("Select FName From [Names] ", myConnection)
'Now to declare a OLEDBDataAdapter object
Dim Adapter As New OleDbDataAdapter
'Now to declare a dataset
Dim DataSet As New DataSet
'Now to apply my Command to the DataAdapter
Adapter.SelectCommand = SearchCmd
DirectReader = SearchCmd.ExecuteReader()
'To bind the Dataset to the DataGrid :)
ComboBoxBinding.DataSource = DataSet
ComboBoxBinding.SelectedText = DirectReader("Fname").ToString
Catch ex As OleDbException
Although I feel that I'm getting close to the answer when I run this I get an Crazy output for my combo Box.
This is what I get in my ComboBox display " vvAASystem.Data.DataViewManagerListItemTypeDescriptor " Now the (vvAA) is actually the LAST primay key set of the FName field in the table [Names] ...so meaning I know my sloppy code was getting close to its objective....but not close enough lol
Thankyou in advance guys for the help and please explain well to this noob if you are willing to give me a helping hand.