How to show one column from database into dropdownlist using dataset?

Posted on 2005-04-10
Last Modified: 2010-04-17
I'm using a dataset in my application.

I want to get use of this dataset to make one column from database appears in drop down list control.

IAnyone has a sample to bind???
Question by:HattanDotNet
    LVL 3

    Assisted Solution

    This example presumes that you have already set up your DataAdapter and DataSet correctly:

    1. Set the DataSource property of the DropDownList to your DataSet

    2. Set the DataMember property of the DropDownList to the table in the dataset you wish to display data from

    3. Set the DataTextField property of the DropDownList to the name of the column you wish to display in the list.

    4. (optional) Set the DataValueField property of the DropDownList to the name of the column you wish to use as value for a selected item in the list. For instance, if you wish to display customers in your DropDown list, DataTextField could be set to the column containing the name of the customer and DataValueField to the column containing the customers ID number.

    5. Add the following code ( example) to the Page_Load event:

            If Not IsPostBack Then
            End If

    (replace SqlDataAdapter1 and DataSet11 with the adapter and dataset you're using in your web application)
    LVL 13

    Accepted Solution

    Here's what I do:

            Dim CS as string
            CS = "Server=<Server_Name>;initial catalog=<Database_Name>;uid=<Username>;pwd=<Password>;"

            Dim myConn as New SqlClient.SqlConnection()
            Dim myAdapter As New SqlClient.SqlDataAdapter()
            Dim myCommand as new SqlClient.SqlCommand()
            Dim dsTemp As New DataSet()

            myCommand.Connection = myConn
            myConn.ConnectionString = CS

            myCommand.CommandText = "<YOUR_SQL_QUERY>"


            myAdapter.SelectCommand = myCommand
            myAdapter.Fill(dsTemp, "Results")

            myDropDownList.DataSource = dsTemp
            myDropDownList.DataMember = "Results"
            myDropDownList.DataTextField = "Customer_Name"
            myDropDownList.DataValueField = "Customer_ID"

            'Add a blank row
            Dim NewDR As DataRow
            NewDR = dsTemp.Tables(0).NewRow
            NewDR(2) = ""

            'Bind the data

            'Initially show blank row in ComboBox.
            myDropDownList.SelectedIndex = (dsTemp.Tables(0).Rows.Count) - 1


    Author Comment

    Thank you guys :)
    In case I used a datareader.
    How can I make the first selected item blank????


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    729 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

    21 Experts available now in Live!

    Get 1:1 Help Now