SqlConnection connection = new SqlConnection();
            connection.ConnectionString = "Data Source = kumar\\SQLEXPRESS; Initial Catalog = phonebook; Integrated Security = TRUE";
            SqlCommand cmd = new SqlCommand("Select * From contacts", connection);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds, "contacts");
            comboBox1.DataSource = ds.Tables["contacts"];
            comboBox1.ValueMember = "phone";
            comboBox1.DisplayMember = "name";
            string pn = comboBox1.ValueMember;
            textBox1.Text = pn;

In this code, combobox1.valuemember is set to phone. I need this value in a text box.The current method is not working for me. What I am getting in the textbox is phone instead of the phone number stored in database. Please help

6 Years
Discussion Span
Last Post by litlemaster

I assume that what you are looking to do is put the phone number from the selected comboBox item into textBox1?

If so then you need to set up a SelectedIndexChanged event handler for the comboBox and in that event handler it should have an assignment of comboBox1.SelectedValue.ToString() to your textBox1.Text.

Something like this:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        textBox1.Text = comboBox1.SelectedValue.ToString();

Otherwise, from what you're doing it almost looks as though you're trying to put EVERY value of the phone column of the data source into your textbox and you're selecting the string "phone" instead of selecting the actual value component of anything.

Hope this helps :) Please mark the thread solved if this resolves your issue.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.