bind columns to dropdownlist

Discussion in 'ASP .Net Web Controls' started by Andy Sutorius, Nov 2, 2004.

  1. How do you tell the dropdownlist datatextfield and datavaluefield to be
    bound to specific columns using the following code?


    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    SqlConnection conn = new
    SqlConnection("server=localhost;trusted_connection=yes;database=Test");
    SqlCommand cmd = new SqlCommand("SELECT * FROM Dogs", conn);
    try
    {
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    DropDownList1.DataSource = reader;
    DropDownList1.DataTextField = ;
    DropDownList1.DataValueField = ;
    DropDownList1.DataBind();
    }
    catch (Exception ex)
    { Response.Write(ex.ToString()); }
    finally
    { conn.Dispose(); }
    }
    }
    Andy Sutorius, Nov 2, 2004
    #1
    1. Advertising

  2. I figured it out. I needed a dataset and then I could set the datatextfield
    and datavaluefield to the fields in the table. Below is the code I ended up
    with.

    private void Page_Load(object sender, System.EventArgs e)

    {

    if (!IsPostBack)

    {

    SqlConnection conn = new
    SqlConnection("server=localhost;trusted_connection=yes;database=Test");

    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Dogs", conn);

    DataSet ds = new DataSet();

    da.Fill(ds, "Dogs");

    try

    {

    conn.Open();

    DropDownList1.DataSource = ds;

    DropDownList1.DataTextField = "Breed";

    DropDownList1.DataValueField = "ID";

    DropDownList1.DataBind();

    }

    catch (Exception ex)

    { Response.Write(ex.ToString()); }

    finally

    { conn.Dispose(); }

    }

    }



    "Andy Sutorius" <> wrote in message
    news:QcThd.41814$...
    > How do you tell the dropdownlist datatextfield and datavaluefield to be
    > bound to specific columns using the following code?
    >
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if (!IsPostBack)
    > {
    > SqlConnection conn = new
    > SqlConnection("server=localhost;trusted_connection=yes;database=Test");
    > SqlCommand cmd = new SqlCommand("SELECT * FROM Dogs", conn);
    > try
    > {
    > conn.Open();
    > SqlDataReader reader = cmd.ExecuteReader();
    > DropDownList1.DataSource = reader;
    > DropDownList1.DataTextField = ;
    > DropDownList1.DataValueField = ;
    > DropDownList1.DataBind();
    > }
    > catch (Exception ex)
    > { Response.Write(ex.ToString()); }
    > finally
    > { conn.Dispose(); }
    > }
    > }
    >
    >
    Andy Sutorius, Nov 2, 2004
    #2
    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,587
    mr2_93
    Oct 2, 2005
  2. Replies:
    2
    Views:
    17,284
  3. Replies:
    0
    Views:
    581
  4. arun.11
    Replies:
    0
    Views:
    939
    arun.11
    Jul 5, 2011
  5. Mike

    Code to bind image in datagrid columns

    Mike, Sep 18, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    302
Loading...

Share This Page