views:

529

answers:

2

i have a dropdownlist that populates from the sql server database. populating the list is not a problem, but does anyone know how to populate the value part of the listitem.

e.g = black blue orange red violet

how do you populate the value=1,2,3,4,5 when you're populating from table in database?

A: 

First you have to build your select statement

Select [ID], [Value] From [Table]

You would store your query into a variable (I use "r" for return) Then you need to attach it to the dropdown

DropDownList1.DataTextField = r.Value
DropDownList1.DataValueField = r.ID
DropDownList1.Databind()

If you really REALLY need to loop, then try something along these lines (not code is not checked, just a general idea.)

For Each o as object in r
  DropDownList1.Items.Insert(o.ID,new ListItem(o.Value,o.ID))
Next

Or with the DataReader (again, untested, but prolly close)

While DataReader.Read()
  DropDownList1.Items.Insert(datareader("value"),new ListItem(datareader("name"),datareader("value"))
End While
rockinthesixstring
ugio
I would recommend NOT looping if you don't have to. Databind is more efficient.
rockinthesixstring
well i have numerous records that come from the database depending on the sql query, so i thought looping with while in a datareader was the right thing to do. so is there no solution but to change the code, or can i work with my existing loop?
ugio
see my edit above. It's prolly not bang on since I'm not sitting in front of a IDE, but it's close.
rockinthesixstring
Another option might be to loop through your records and add them to your own custom object, then bind your DropDownList to the custom Object.
rockinthesixstring
see i did that. dropdownlist.items.add(datareader("name","value")and the result i got was Black1, blue2 ... as concatenated
ugio
I used Items.Insert, not Items.Add. You also need to add it DropDownList1.Items.Insert(datareader("value"),new ListItem(datareader("name"),datareader("value"))... or something like that
rockinthesixstring
o man ur right. thanks for being patient man.
ugio
don't forget to mark as answer ;)
rockinthesixstring
done. thats one more feather in ur cap
ugio
getting close to my 1000 goal... shootin for the end of the weekend ;)
rockinthesixstring
how do i become better in vb.net? i need to work on it evreyday, so i wanna be really good at it
ugio
It all depends on how deep you want to go. If this is a career and your full time job... you'll prolly want to go to school. If you want to do it on your own... Google is your friend. Come up with an idea or a challenge and work your way through it.
rockinthesixstring
A: 

Assuming that you have an IList implementation which you are binding the data objects to (you seem to have the results materialized in something already, as you say you can populate the text), what you need to do is specify the name of the property/member on your instances returned from your IList implementation.

So, assuming that you have a DataTable with columns of ID and Value, or an IList of objects that was populated from LINQ-to-SQL/Entities, each with an ID property and a Value property, you would do this:

DropDownList1.DataValueField = "ID"
DropDownList1.DataTextField = "Value"

You need to set it to the names of the fields as opposed to the values, as the binding mechanism uses the property descriptor framework to figure out how to map those strings to the properties/columns which contain the data.

casperOne