[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2005-04-10
Medium Priority
Last Modified: 2010-04-17
I'm using a dataset in my ASP.net 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

Assisted Solution

Chipzter earned 1000 total points
ID: 13747858
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 (VB.net 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

Torrwin earned 1000 total points
ID: 13756841
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

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


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Simple Linear Regression
Six Sigma Control Plans

873 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