views:

412

answers:

2

How do I display a table value in a listbox control? From this code I got the ProjectCode value from the TeamMember_table:

SqlDataAdapter adp = new SqlDataAdapter("select ProjectCode from TeamMember_table where EmpId = '" + eid + "'", conn);

I want to display this ProjectCode value in a listBox.

Platform: Visual Studio 2008, ASP.NET with C#.

+2  A: 

Using the ASP.NET ListBox control you can do it by assigning the DataSource property:

 ListBox1.DataSource = YourDataSource;
        ListBox1.DataTextField = "projectCode";
        ListBox1.DataBind();

Where "YourDataSource" can be a DataTable filled with data from your SqlDataAdapter.

You can also add every item manually:

ListItem item = new ListItem();
            item.Text="sometext";
            item.Value="somevalue";
            ListBox1.Items.Add(item);

You can read more about data binding and the ASP.NET ListBox control here.

Misha N.
A: 

but while adding manually items with same values only the first one will get inserted. how we can overcome it?

jk
This really should have been a comment in Peter's answer.
Walter
Peter's code would just need to be added to code looping through all the records.
Walter