Groups | Blog | Home
all groups > vb.net controls > november 2004 >

vb.net controls : populating dropdownlist from db


paul
11/23/2004 9:16:33 PM
I've been fighting with this for a solid day now.

I want to populate a dropdownlist with items from a table in design mode.

seems like a simple thing, but I can't make it happen. Could someone please
aim me at some good documentation on this? Sorry, I'm a dot.newbie but not
a new programmer & I'm pretty frustrated about now.

Thanks in advance.

-Paul



paul
11/23/2004 11:01:24 PM
the answer was so simple, but after 15 hours of looking it wasn't anywhere
on the web. I'm posting it here so hopefully some other newb doesn't waste
as much time as me.

How To Populate a DropDownList from a database in asp.net - mostly in design
mode.

Drag and drop + 6 lines of code.

1. create db connection in server explorer
2. expand that connection
3. drag and drop the table with which to populate the dropdownlist to the
form
4. click Data>Generate Dataset
4. drag and drop DropDownList fom Web Forms Toolbox to the form
5. Add this code to Private Sub Page_Load

This assumes the table which holds the values you want to populate the list
with is called yourTable, the column which holds the data you want to
display is called lastName, and the value you want to store is called ID.

OleDbDataAdapter1.Fill(DataSet11)
DropDownList1.DataSource = DataSet11
DropDownList1.DataMember = "yourTable"
DropDownList1.DataTextField = "lastName"
DropDownList2.DataValueField = "ID"
DropDownList1.DataBind()


paul
11/24/2004 12:49:58 AM
typo correction:

DropDownList1.DataValueField = "ID"

AddThis Social Bookmark Button