Dropdown List data binding

Discussion in 'ASP .Net' started by Binod Nair, Jan 16, 2004.

  1. Binod Nair

    Binod Nair Guest

    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
     
    Binod Nair, Jan 16, 2004
    #1
    1. Advertising

  2. Binod Nair

    Michael Guest

    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

    "Binod Nair" <> wrote in message
    news:...
    > 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
    >
    >
     
    Michael, Jan 16, 2004
    #2
    1. Advertising

  3. Binod Nair

    Binod Nair Guest

    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.


    "Michael" <raterus@localhost> wrote in message
    news:%23%...
    > 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
    >
    > "Binod Nair" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
     
    Binod Nair, Jan 16, 2004
    #3
  4. Binod Nair

    Hermit Dave Guest

    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
    "Binod Nair" <> wrote in message
    news:...
    > 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
    >
    >
     
    Hermit Dave, Jan 16, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    25,060
    Fraggle
    Sep 14, 2004
  2. =?Utf-8?B?ZGF2aWQ=?=

    Data binding in TextBox after dropdown list indexchanged

    =?Utf-8?B?ZGF2aWQ=?=, Apr 1, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    3,578
    Karl Seguin
    Apr 4, 2005
  3. keithb
    Replies:
    1
    Views:
    18,942
    Eliyahu Goldin
    Apr 4, 2006
  4. =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Trouble binding a dropdown list in a detailsview.

    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Nov 16, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,555
    B. Chernick
    Nov 24, 2007
  5. Mike Milian
    Replies:
    0
    Views:
    343
    Mike Milian
    Apr 14, 2008
Loading...

Share This Page