Link to home
Start Free TrialLog in
Avatar of expertsexchangehunter
expertsexchangehunter

asked on

How do I populate a Visual Studio 2003 dropdownlist

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

Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

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
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
Avatar of expertsexchangehunter
expertsexchangehunter

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?