-1

I have to bind the combo box with the following code:

private void getCompanydata()
{
    MySqlConnection con = new MySqlConnection(ConfigurationManager.AppSettings["RL_InventoryConnection"]);
    if (con.State == ConnectionState.Closed)
        con.Open();
    MySqlCommand cmd = new MySqlCommand("select comp_id, concat(comp_name,'-', comp_add) as company from companymaster;", con);
    MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);

    DataRow dr;
    dr = dt.NewRow();
    dr.ItemArray = new object[] {0, "--Select Delivery Location--" };
    dt.Rows.InsertAt(dr, 0);

    comboBox1.DisplayMember = "company";
    comboBox1.ValueMember = "comp_id";
    comboBox1.DataSource = dt;
}

In another method, I want to access comp_id which is bound with valueMember. I am trying with the following code, but it’s not working:

private void SaveData()
{
  string company = comboBox1.Text.ToString();
  int companyid = Convert.ToInt32(comboBox1.SelectedValue);
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65

1 Answers1

0

You want to try use combobox1.SelectedValue instead of combobox1.MemberValue.

More Details in the documentation here... https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listcontrol.selectedvalue?view=windowsdesktop-6.0#system-windows-forms-listcontrol-selectedvalue

Similar answer link below... https://stackoverflow.com/a/6901118/4462984

  • hi @Nicholas Sims, this will give DisplayMember value. i want to get the valuemember. – Mohd Sameer Sep 01 '22 at 13:37
  • @MohdSameer, sorry if that's not working for you. Have you tried it in your project? Documentations states it gets/sets the MemberValue property... https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox?view=windowsdesktop-6.0#:~:text=Gets%20or%20sets%20the%20value%20of%20the%20member%20property%20specified%20by%20the%20ValueMember%20property. – Nicholas Sims Sep 01 '22 at 14:40