Bomboe Cristian
Bomboe Cristian

Reputation: 5

How to Populate ComboBox from access db in C#

I have the next combobox:

this.comboBoxProd.Enabled = false;
this.comboBoxProd.FormattingEnabled = true;
this.comboBoxProd.Items.AddRange(new object[] {
            "Cameras",
            "------------",
            " Digital IXUS 850 IS ",
            " Digital IXUS 900 Ti ",
            " Digital IXUS 75 -NEW- ",
            " Digital IXUS 70 -NEW- ", etc.

I want to change it and take the values from a db. My database name is bd1.mdb and in the table Cameras it has the following fields: CamID, Cameras, Warranty, Year. I am only interested in the "Cameras" field. Thank you!

Upvotes: 0

Views: 14613

Answers (2)

Spyder045
Spyder045

Reputation: 121

Probably want to add a valuemember to the combobox this way you can link back to the camera id. Try changing the query to:

SELECT CameraID, Cameras From Camers

Then add the cameraID to the value member

combobox.valuemember = "cameraID"

and

combobox.displaymember = "Cameras".

Upvotes: 1

Myra
Myra

Reputation: 3656

You should take a closer look for ADO.NET operations with .mdb files here

First,prepare your connection string

string connString = "Microsoft.Jet.OLEDB.4.0;Data Source=C:\\bd1.mdb";

Next step is to prepare your query

string query = "SELECT Cameras FROM Cameras";

You will need an adapter to bind datasource,in your case it's OleDbDataAdapter

OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

Now you can use a DataTable object to bind into combobox

DataTable source = new DataTable();

Fill data into your source

dAdapter.Fill(source);

Your source is full with Cameras,now you can refer to your combobox control

combobox.DataSource = source;

DO NOT FORGET THAT you should that which field will be displayed in Combobox items

combobox.DataTextField = "Cameras";//from query

Upvotes: 5

Related Questions