Sameer Farooqui

Sameer Farooqui

  • NA
  • 58
  • 3.5k

how to access valuemember of the combobox

Sep 1 2022 1:08 PM

I have 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 bind with valueMember. I am trying with following code but its not working.

private void SaveData()
{
    string company = comboBox1.Text.ToString();
    int companyid = Convert.ToInt32(comboBox1.SelectedValue);
}

 


Answers (2)