SqlConnection connection = new SqlConnection();
            connection.ConnectionString = "Data Source = kumar\\SQLEXPRESS; Initial Catalog = phonebook; Integrated Security = TRUE";
            connection.Open();
            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

Recommended Answers

All 2 Replies

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.

many a thanks to you... This worked very fine.

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.