Dropdown List data binding

B

Binod Nair

Hi All,

This is what I am trying to do.I have an aspx page with the the following
code block.

<asp:DropDownList id="DropDownList1" runat="server" DataSource="<%#
GetUsers%>" DataTextField="user_email" DataValueField="user_id">
</asp:DropDownList>

GetUsers is defined in the .vb file

Public Function GetUsers() As DataSet

Dim ds As DataSet
ds =
SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings("ConnectionString
"), CommandType.Text, "select user_id , user_email from users")
Return ds

End Function


I am kind of frustrated that I cannot make this thing work.What am I doing
wrong ?

--binod
 
M

Michael

I never have liked binding data using <%# %>, because it sure looks old
asp'ey to me. So I'll tell you how I would do it. I've never seen that
SqlHelper object either, but I'll assume it is returning a valid dataset.

in your page_load, (or anywhere else really)
add this

DropDownList1.dataSource = GetUsers()
DropDownList1.DataTextField = "user_email"
DropDownList1.DataValueField = "user_id"
DropDownList1.DataBind()

then in your aspx file, where you want the DropDown you can just put

<asp:DropDownList id="DropDownList1" runat="server" />

where you want the dropdown.
and it should work.

Another thing too, depending on what you are doing, you may want to just use
a sqldatareader to populate this box, instead of passing around a dataset,
especially if you have no intention of ever editing the items in the box.

--Michael
 
B

Binod Nair

I know its ASP style.But I am not sure why is this not working.
SQLHelper is the Microsoft Dataccess Class whcih does return a valid
Dataset.

The way u said , it works fine.I just want to make it work like this.Any
clues why is it not returning any data.
 
H

Hermit Dave

do this
<asp:DropDownList id="DropDownList1" runat="server">

in your code behind file
if(!Page.IsPostBack)
{
DataSet myDS = GetUsers();
DropDownList1.DataSouce = myDS;
DropDownList1.DataTextField =
myDS.Tables[0].Columns["user_email"].toString();
DropDownList1.DataValueField =
myDS.Tables[0].Columns["user_id"].toString();
DropDownList1.DataBind();
}


this is c# code convert to equivalent VB code

--
Regards,

HD

Once a Geek.... Always a Geek
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top