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

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???
HattanDotNetAsked:
Who is Participating?
 
TorrwinCommented:
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>"

        myConn.Open()

        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) = ""
        dsTemp.Tables(0).Rows.Add(NewDR)
        '=========================================================================

        'Bind the data
        myDropDownList.DataBind()

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

        myConn.Close()
0
 
ChipzterCommented:
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
            SqlDataAdapter1.Fill(DataSet11)
            DataBind()
        End If
------------

(replace SqlDataAdapter1 and DataSet11 with the adapter and dataset you're using in your web application)
0
 
HattanDotNetAuthor Commented:
Thank you guys :)
In case I used a datareader.
How can I make the first selected item blank????

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.