Newbie databind dropdownlist question

D

dw

Hello, all. I'm just starting to work with ASP.NET in VS.NET 2003. I've
created a page with a dropdownlist server control (lstStates) that should
pull in the contents of an Access table tblValidStateAbbr (has states and
their abbreviations). However, no matter what I do, I can't get the dropdown
to show any data on the page.

I have a connection, an OLE DB data adapter, and a dataset that contains the
SELECT to the tblValidStateAbbr. All were created via double-clicking the
data objects on the DATA tab of the Toolbox and following the wizards.

In the pageload event of the code behind file, I have this code. I've been
doing years of ASP development, and ASP.NET is new to me. I think I'm
missing something simple. Any help is appreciated. Thank you.

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Me.OleDbDataAdapter1.Fill(Me.DsStates1)
Me.lstStates.DataBind()

End Sub
 
D

dw

Thanks, Scott, for your reply. I set the Data Source of the drop down by
selecting it in Design View and going to the the Property window, and simply
selecting the dsStates1 dataset. That's also where I set the DataTextField
and DataValueField to "state" and "abbr" (without the quotations).
 
D

dw

Scott, I got it! I had set the Data Member property for the drop down in the
Property window to tblValidStateAbbr. Once I cleared that, it worked.
Apparently you can't have both the Data Source and Data Member with values
in them. Thanks for your help.
 
D

dw

Scott, I've got another question: How do I use the same data adapter to pull
in another table unrelated to the states table? Thanks.
 
S

Scott M.

A DataAdapter works based on its Command Objects (Select, Insert, Update &
Delete) that are child objects of the DataAdapter object. If you want to
pull data from a different table in, you'll either have to change the Select
Command of the DataAdapter to a new SQL statement (or stored procedure) and
re-fill your DataSet or use a new DataAdapter.
 
S

Scott M.

I have found that it is not a good idea to set the DataSource, DataTextField
or DataMember properties using the properties window because they do cause
problems when you want to do something later in code that requires a
different setting for these, so if I am going to create the object using
design techniques, I set these values in code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top