Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a two column table in an MS Access database and want to fill its second column in a ComboBox from where the user can make a selection. I'm using a DataTable to do it. The code is below:

C#
private void Form1_Load(object sender, EventArgs e)
{
    OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data         Source=C:\Users\Anas\Dropbox\C# Projects\2.Database\ClOsODb.accdb; Persist Security   Info=False");
    connect.Open();
    OleDbCommand cmd = new OleDbCommand("select * from Course Number and Name", connect);
    OleDbDataReader odr = null;
    odr = cmd.ExecuteReader();
    DataTable table = new DataTable();
    table.Load(odr);
    course_Number_and_NameComboBox.DataSource = table;
    course_Number_and_NameComboBox.BindingContext = this.BindingContext;
    course_Number_and_NameComboBox.DisplayMember = "Course Number and Name";
    course_Number_and_NameComboBox.ValueMember = "ID";
    connect.Close();
}


However, the problem is that when I run the application, the ComboBox is empty! It doesn't get filled. Can anyone help me and tell me where I am wrong and what should I do? Help will be greatly appreciated.

Also, can anyone tell me how will I get the value in the first column of the selected row of the DataTable when a selection is made at the ComboBox(in other words, when an item will be selected in the ComboBox-second column of a row in the table-how will I know which row is selected in the ComboBox and how will I get the value in the first column of the same row)?
Posted
Comments
DamithSL 5-Jul-14 10:25am    
what is your access Table name?
what are the column names?

Firstly have a look at that sql command
select * from Course Number and Name
What is the name of the table? As far as SQL is concerned then it is "Course" and "Number and Name" just throw an error - which is why you don't get your combobox populated.

If you really must use spaces in your table names ( = bad idea ) then you must surround the table name with square brackets, so the query becomes
select * from [Course Number and Name]


The same applies to the column of the same name
C#
course_Number_and_NameComboBox.DisplayMember = "[Course Number and Name]";


As to the second part of your question, when a row is selected in the ComboBox the course_Number_and_NameComboBox.SelectedText property will be set to the visible text (i.e. the value that was in [Course Number and Name]) and the course_Number_and_NameComboBox.SelectedValue property will be set to the ValueMember - i.e. the value that was in ID
If you want any other data from the datatable you will have to look it up based on the ID
 
Share this answer
 
Comments
Anas Tasadduq 5-Jul-14 10:57am    
Thanx very much for your help. A very clear answer. And that's what I like!
Anas Tasadduq 5-Jul-14 11:03am    
But the ComboBox still does not populate. :(
change
C#
OleDbCommand cmd = new OleDbCommand("select * from Course Number and Name", connect);

to
C#
OleDbCommand cmd = new OleDbCommand("select Col1, ID from [Course Number and Name]", connect);

give the column names as you given in your table and also use [] when you have spaces in your table name or column names
then,
C#
course_Number_and_NameComboBox.DisplayMember = "Col1";
course_Number_and_NameComboBox.ValueMember = "ID";
 
Share this answer
 
Comments
Anas Tasadduq 5-Jul-14 11:05am    
This is also not working! :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900