how can I bind the dataset to a dropdownlist, i want to use the field "product id" as  the value,  and "product name" as the  label

Posted on 2004-10-28
Last Modified: 2009-12-16
i have a function to return the value from a sql statement "select * from product"

how can I bind the dataset to a dropdownlist, i want to use the field "product id" as  the value,  and "product name" as the  label
Question by:musclejack
    LVL 18

    Accepted Solution

    You can bind it like following:

    DropDownList1.DataSource = ds;
    DropDownList1.DataTextField = "ProductName";
    DropDownList1.DataValueField = "ProductID";

    LVL 17

    Expert Comment

    and if you ever needed it for a datareader:
    'where dr is your datareader
     dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            Me.ddlAssignedTechnician.DataSource = dr
            Me.ddlAssignedTechnician.DataTextField = "LastName"
            Me.ddlAssignedTechnician.DataValueField = "TechnicianID"
    LVL 8

    Expert Comment

    tusharshah is right.. so is AerosSaga if you want to use a data reader..

    Just remember to bind it within if(!IsPostBack).. block.. so that the data binding occurs only the first time the page loads, and not on every postback! If binding occurs on evey page load, you would lose the SelectedItem/SelectedIndex value.

       DropDownList1.DataSource = ds;
       DropDownList1.DataTextField = "ProductName";
       DropDownList1.DataValueField = "ProductID";
    LVL 10

    Expert Comment

    Aeros and Daffodils are right on this.  Populating a drop down is always a straight forward query of data and so you definitely should be using a data reader rather than a dataset.  Datasets are for when you need to get data into a grid where you'll be sorting, updating, etc. the data.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now