Solved

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
1,988 Views
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
0
Question by:musclejack
    4 Comments
     
    LVL 18

    Accepted Solution

    by:
    You can bind it like following:

    DropDownList1.DataSource = ds;
    DropDownList1.DataTextField = "ProductName";
    DropDownList1.DataValueField = "ProductID";
    DropDownList1.DataBind();

    -tushar
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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"
            Me.ddlAssignedTechnician.DataBind()
    0
     
    LVL 8

    Expert Comment

    by:daffodils
    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.

    if(!IsPostBack)
    {
       DropDownList1.DataSource = ds;
       DropDownList1.DataTextField = "ProductName";
       DropDownList1.DataValueField = "ProductID";
       DropDownList1.DataBind();
    }
    0
     
    LVL 10

    Expert Comment

    by:jnhorst
    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.

    John
    0

    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 (http://www.enterprise.efax.com), 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