Question

I'm Trying to populate a Combo Box using C# and MS Access 2013 i've looked around the web but so far here what i was able to do

1 - I created a function to fill the ComboBox with as the following :

public static void FillDropDownList(string Query, System.Windows.Forms.ComboBox DropDownName)
{
    using (var CONN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\Vendors.accdb;"))
    {
        CONN.Open();
        DataTable dt = new DataTable();
        try
        {
            OleDbCommand cmd = new OleDbCommand(Query, CONN);
            OleDbDataReader myReader = cmd.ExecuteReader();
            dt.Load(myReader);
        }
        catch (OleDbException  e)
        {
            Console.WriteLine(e.ToString());
            Console.ReadLine();

            return;
        }
        DropDownName.DataSource = dt;
        DropDownName.ValueMember = "BRANCH_CODE";
        DropDownName.DisplayMember = "BRNCH_NAME";
    }
}

2- after that i called it in the Form Load event Like this :

private void VendorMain_Load(object sender, EventArgs e)
{
    FillDropDownList("select BRANCH_CODE,BRNCH_NAME from BRANCHES", BranchCB);
}

but after initializing the programme the ComboBox is still empty and not populated

So what Should i Do ?

Was it helpful?

Solution 2

@Steve

you where correct there was a mis-spell issue in the Ms Access Database and there was a space between BRNACH_CODE and it was suppose to be [BRANCH CODE]

thank you

OTHER TIPS

I am unable to recreate your issue. With test data in a table named [BRANCHES]

BRANCH_CODE  BRNCH_NAME 
-----------  -----------
          1  Main Street
          2  at the mall

I copied and pasted your code into my C# project and the Combo Box is populated correctly:

ComboBox.png

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top