Dear Experts

I use following codes to diplay data in combobox

str = "SELECT sno,name,city FROM employees"
cmd = New SqlClient.SqlCommand(str, con)
da = New SqlClient.SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)

With ComboBox1
.DataSource = dt
.DisplayMember = "name"
.ValueMember = "sno"
.SelectedIndex = 0
End With

Table has three fields as sno,name,city
Combobox displays name column
and data in table is as

sno--name-----city
1-------a------london
2-------b------moscow
3-------c-------tehran

In textbox1, I want to display sno against selected name
for example

If combobox1. text is=A
then
textbox1.text=1

please help

Recommended Answers

All 3 Replies

str = "SELECT sno,name,city FROM employees"
cmd = New SqlClient.SqlCommand(str, con)
da = New SqlClient.SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)

With ComboBox1
.DataSource = dt
.DisplayMember = "name"
.ValueMember = "sno"
.SelectedIndex = 0
End With
'The following line needs to be added to get the correct value into the text box after the intial databinding
TextBox1.Text = ComboBox1.SelectedValue.ToString
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        TextBox1.Text = ComboBox1.SelectedValue.ToString
End Sub
str = "SELECT sno,name,city FROM employees"
cmd = New SqlClient.SqlCommand(str, con)
da = New SqlClient.SqlDataAdapter(cmd)
dt = New DataTable
da.Fill(dt)

With ComboBox1
.DataSource = dt
.DisplayMember = "name"
.ValueMember = "sno"
.SelectedIndex = 0
End With
'The following line needs to be added to get the correct value into the text box after the intial databinding
TextBox1.Text = ComboBox1.SelectedValue.ToString
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        TextBox1.Text = ComboBox1.SelectedValue.ToString
End Sub

Dear Sir,

supose combobox text is =a

then how to display following corresponding data

textbox1.text=1
textbox2.text=london

What I posted works for a single value retrieved from the combo box, however if you are looking to populate multiple values based on a combo box selection it becomes a bit more involved. I am assuming you are wanting to use dynamic data from the database instead of the static data in your example. For this reason the idea of using IF or Case statements to directly assign values to the text box in your original post would not work unless you know every value that could possibly be added to the datatable. Population of the textbox data needs to be done by referencing the data, not direct string assingment.

The way to approach this is generally going to be to have the combo box store the primary key of your datatable as it's value member, and have a function that returns a row of data based on the primary key. You can then assign the values of this data row to any particular text box you want. In otherwords databinding to the combo box will only take you so far.

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.