Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am using lisbox to display the database value....if i select the particular item from listbox i just want to display the product code in textbox.......

This is my code to display the database value into listbox
private void productlist_Load(object sender, EventArgs e)
            SqlCommand cmd = new SqlCommand("select productcode,productname from products", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                listBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString() + "\t\t" + ds.Tables[0].Rows[i][1].ToString());



how to get the productcode in textbox.........
Member 11690281 20-Jan-17 1:26am    
how to get the productcode in textbox.........
Member 11690281 20-Jan-17 1:26am    
how to get the product code in textbox

You may use the ListBox.SelectItem property. You may find a code sample in the documentation[^].
Share this answer
Hi, use below code:

textBx1.Text = ListBox1.SelectedItem.Value;
Share this answer
You may use the below code to display a single value:

If you want to select multiple values then u may need for loop:
for i=0 to listbox1.items.count-1
if listbox1.items(i).selected=true then
end if
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900