We help IT Professionals succeed at work.

How do I populate a Visual Studio 2003 dropdownlist

Medium Priority
307 Views
Last Modified: 2012-05-06
How do I populate the dropdownlist using
dataTestfield and datavaluefield?
I have included code that does noit populate the list
Dim sqlConnection As New SqlConnection(CStr(Application("TestConnString")))
 
        Dim strSQL As String = "procGetSales"
        Dim sqlCommand As New SqlCommand
 
        Try
 
            Try
                sqlConnection.Open()
            Catch exc As Exception
                lblMessages.Text = exc.Message()
            End Try
            sqlCommand.CommandText = strSQL
            sqlCommand.Connection = sqlConnection
            sqlCommand.CommandType = CommandType.StoredProcedure
            sqlCommand.Parameters.Add("@nvchrAction", "GET SALES")
 
            Dim rdrMyQuery As SqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
 
            'Dim temo As 
            cboSales.DataSource = rdrMyQuery
            cboSales.Items.Add("Select from List")
            Do While rdrMyQuery.Read
                'cboSales.Items.Add(rdrMyQuery.GetString(0))
                cboSales.DataTextField = rdrMyQuery.GetString(0)
                cboSales.DataValueField = CStr(rdrMyQuery.GetInt32(1))
            Loop
            cboSales.DataBind()
            rdrMyQuery.Close()
        Catch
            lblMessages.Text = "<font color='RED'>" + Err.Description + "</font>"
        End Try
        sqlCommand.Dispose()
        sqlConnection.Close()
        sqlConnection.Dispose()

Open in new window

Comment
Watch Question

TimCotteeHead of Software Services

Commented:
Hello expertsexchangehunter,

You are mixing and matching your methods here.

You need to either do:

            Dim rdrMyQuery As SqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
 
            'Dim temo As
            cboSales.DataSource = rdrMyQuery
            cboSales.DataTextField = "The FieldName In The Reader That Should be displayed"
            cboSales.DataValueField = "The Fieldname in the reader that should be used as the value property for the item, can be the same as the text field above"
            cboSales.Items.Insert(0, New ListItem("Select from List","Select"))
            cboSales.DataBind()
            rdrMyQuery.Close()

Or

            Do While rdrMyQuery.Read
                cboSales.Items.Add (New ListItem(rdrMyQuery.GetString(0),CStr(rdrMyQuery.GetInt32(1)))
            Loop

Regards,

TimCottee
TimCotteeHead of Software Services

Commented:
expertsexchangehunter,

In the second instance you do not need the .DataBind() because you are explicitly adding the items to the collection, in the first case you do need to bind because you are binding the specified fields to the text and value properties of the items collection.

TimCottee
I used the first solution with the datatestfield and datavaluefield and the list is properly populated but when I go to add the selected datavaluefield it contains the name I used for
            cboSales.DataValueField = "The Fieldname in the reader that should be used as the value property for the item, can be the same as the text field above"

when it should be the value from the database table for the specific record. What am I doing wrong?
Head of Software Services
Commented:
You set them as appropriate, if you have for example ID and Description as columns in your resultset then set

.DataValueField = "ID"
.DataTextField = "Description"

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
I have
              cboSales.DataSource = rdrMyQuery
            cboSales.DataTextField = "SalesName"
            cboSales.DataValueField = "intSalesPersonID"
            cboSales.Items.Insert(0, New ListItem("Select from List", "Select"))
            cboSales.DataBind()
            rdrMyQuery.Close()
I changed the select statement to use ID for the datavalue field
Also the select uses as to set the values
select dsds and sdsds as salesnem and dfdfds as ID
chould this make a difference?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.