Bind a SQL table to a dropdownlist at design time? Surely it can be done?

Discussion in 'ASP .Net' started by GG, May 24, 2005.

  1. GG

    GG Guest

    I have a typical ASP page with a dropdownlist web form control. I want
    this to auto-populate from a column in a sql table.

    I've tried dragging a sql connection data control onto the form, played
    with data binding, everything! I'm sure this is easy, can anyone point
    out the steps I need to take?

    Thanks ...
    GG, May 24, 2005
    #1
    1. Advertising

  2. GG

    Brock Allen Guest

    Here's an example:

    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
    using (SqlConnection cn = new SqlConnection("server=localhost;database=pubs;trusted_connection=yes;"))
    {
    using (SqlCommand cmd = cn.CreateCommand())
    {
    cmd.CommandText = "select * from authors";
    cn.Open();
    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
    _ddl1.DataSource = rdr;
    _ddl1.DataTextField = "au_fname";
    _ddl1.DataBind();
    }
    }
    }
    }
    </script>
    <body>
    <form id="form1" runat="server" >
    <asp:DropDownList runat="server" ID="_ddl1"></asp:DropDownList>
    </form>
    </body>
    </html>


    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I have a typical ASP page with a dropdownlist web form control. I want
    > this to auto-populate from a column in a sql table.
    >
    > I've tried dragging a sql connection data control onto the form,
    > played with data binding, everything! I'm sure this is easy, can
    > anyone point out the steps I need to take?
    >
    > Thanks ...
    >
    Brock Allen, May 24, 2005
    #2
    1. Advertising

  3. RE: Bind a SQL table to a dropdownlist at design time? Surely it can b

    Hi!

    Dude, hope that u can get the values in a DataSet from the DataBase.Now
    things are pretty starightfwd from here
    just put in this code


    if(objDs.Tables.Count != 0) //objDs is the dataset which has the table
    Employee
    {
    if(objDs.Tables[0].Rows.Count != 0)
    {
    ddlEmployeeId.DataSource = objDs;
    ddlEmployeeId.DataValueField = "EmployeeID";
    ddlEmployeeId.DataTextField = "EmpName";
    ddlEmployeeId.DataBind();
    }
    }




    "GG" wrote:

    > I have a typical ASP page with a dropdownlist web form control. I want
    > this to auto-populate from a column in a sql table.
    >
    > I've tried dragging a sql connection data control onto the form, played
    > with data binding, everything! I'm sure this is easy, can anyone point
    > out the steps I need to take?
    >
    > Thanks ...
    >
    >
    =?Utf-8?B?TWF2ZXJpY2s=?=, May 25, 2005
    #3
  4. GG

    mail2saion

    Joined:
    May 14, 2009
    Messages:
    2
    Hello readers today i found a helpful post on how you can optimize your code to bind with Dropdown List:

    shawpnendu.blogspot.com/2009/05/how-to-bind-or-populate-data-into.html

    HOPE IT WILL HELP READERS.
    mail2saion, May 14, 2009
    #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. mr2_93
    Replies:
    1
    Views:
    3,582
    mr2_93
    Oct 2, 2005
  2. Andoni
    Replies:
    0
    Views:
    320
    Andoni
    Apr 26, 2004
  3. Groove
    Replies:
    1
    Views:
    629
    Nathan Sokalski
    Jun 1, 2006
  4. gertrude
    Replies:
    1
    Views:
    265
    Gianni Mariani
    Jan 4, 2007
  5. Chris Stankevitz
    Replies:
    13
    Views:
    2,449
    88888 Dihedral
    Feb 21, 2012
Loading...

Share This Page