I'm newbie to C# so please bear with me for that issue and just to make the issue clear i'm gonna start from the very beginning of the My code What i wanted to do @ the beginning that i wanted to populate my comboBox from MS ACCESS database but with both a value to hold in the combobox and a value to display as the following : i wanted to populate the combobox with the Empolyer ID but make the combobox displaye the name as showen in the pic 9dd3700d7a115db61e09ba00faff356a

public static void FillDropDownList(string Query, System.Windows.Forms.ComboBox DropDownName, string AValue, string Adisplay)
                {
                    string Value = AValue;
                    string display = Adisplay;

                    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);
                            /// MessageBox.Show (dt.Rows.Count.ToString());
                            // Console.ReadLine();


                        }
                        catch (OleDbException e)
                        {
                            MessageBox.Show(e.ToString());
                            // Console.WriteLine(e.ToString());
                            // Console.ReadLine();

                            return;
                        }
                        DropDownName.DataSource = dt;
                        DropDownName.ValueMember = Value;
                        DropDownName.DisplayMember = display;
                    }
                }

And i call it in the load like the following

private void VendorMain_Load(object sender, EventArgs e){FillDropDownList("select EMPCODE,EMPNAME from EMPLOYERS ", EMP_CB , "EMPCODE", "EMPNAME ");}

and i thought that everything worked perfectly until the point i wanted to only add the Value member in another table in the database so when i tried to added using the insert SQL statement i found that the value member that is being added is the text "EMPCODE" not the value of the code it self { 1 or 2 or 3 ,,,,,,, },so does any body have a solution for this ........ ???

On your combobox, use either event "SelectedValueChanged" or "SelectedIndexChanged".

SelectedValueChanged

        private void EMP_CB_SelectedValueChanged(object sender, EventArgs e)
        {
            Console.WriteLine("ValueMember A: " + EMP_CB.ValueMember.ToString() + " DisplayMember A: " + EMP_CB.DisplayMember.ToString());

            //---------------------------
            //Get data for selected item
            //---------------------------
            DataRow selectedDataRow = ((DataRowView)EMP_CB.SelectedItem).Row;
            int empId = Convert.ToInt32(selectedDataRow["EMPCODE"]);
            string empName = selectedDataRow["EMPNAME"].ToString();

            Console.WriteLine("empId A: " + empId + " empName A: " + empName);
            Console.WriteLine();
        }

SelectedIndexChanged

        private void EMP_CB_SelectedIndexChanged(object sender, EventArgs e)
        {
            Console.WriteLine("ValueMember B: " + EMP_CB.ValueMember.ToString() + " DisplayMember B: " + EMP_CB.DisplayMember.ToString());

            //---------------------------
            //Get data for selected item
            //---------------------------
            DataRow selectedDataRow = ((DataRowView)EMP_CB.SelectedItem).Row;
            int empId = Convert.ToInt32(selectedDataRow["EMPCODE"]);
            string empName = selectedDataRow["EMPNAME"].ToString();

            Console.WriteLine("empId B: " + empId + " empName B: " + empName);
            Console.WriteLine();
        }

The code is the same inside both.

Taken from: Here and Here

commented: that One Answered it +0

When you insert a breakpoint you can step your code. Highlight and rightclick the combobox you can then inspect the Value and Display members and check if they are correct.

I think you may try what "cgeier" has suggested. Hope that would help you.

cgeier thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.