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

x
?
Solved

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

Posted on 2005-04-10
3
Medium Priority
?
231 Views
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???
0
Comment
Question by:HattanDotNet
3 Comments
 
LVL 3

Assisted Solution

by:Chipzter
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
            SqlDataAdapter1.Fill(DataSet11)
            DataBind()
        End If
------------

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

Accepted Solution

by:
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>"

        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
 

Author Comment

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

0

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