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
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()
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
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
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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()
cboSales.DataSource = rdrMyQuery
cboSales.DataTextField = "SalesName"
cboSales.DataValueField = "intSalesPersonID"
cboSales.Items.Insert(0, New ListItem("Select from List", "Select"))
cboSales.DataBind()
rdrMyQuery.Close()
ASKER
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?
Also the select uses as to set the values
select dsds and sdsds as salesnem and dfdfds as ID
chould this make a difference?
You are mixing and matching your methods here.
You need to either do:
Dim rdrMyQuery As SqlDataReader = sqlCommand.ExecuteReader(C
'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.GetStr
Loop
Regards,
TimCottee