How to populate VB.NET multidimensional dropdownlist?
I am trying to initialize a dropdownlist in VB.NET but my dropdownlist is not populating any values. I want the DataTextField to be different from DataValues in the dropdownlist. Dropdownlist should display series of strings; DataValues should only be numbers.
How can I implement this? Here is my code excerpt right now:
Dim ProductList As New ArrayList()
Dim ProdCodeSearch As String
Dim InstrumentSearch As String
pcSQL = " select distinct instrument_name AS [instrument_name], product_code AS [product_code] from FRUD.tblXref order by instrument_name "
D开发者_C百科im DBConn As SqlConnection
DBConn = New SqlConnection(ConfigurationManager.AppSettings("AMDMetricsDevConnectionString"))
DBConn.Open()
Dim reader As SqlDataReader
Dim DBCommand As New SqlCommand(pcSQL, DBConn)
reader = DBCommand.ExecuteReader()
While reader.Read()
End While
dProdCodeSearch.Items.Add(reader(0))
dProdCodeSearch.DataTextField = "instrument_name"
dProdCodeSearch.DataValueField = "product_code"
dProdCodeSearch.DataBind()
reader.Close()
Could you use something like this?
While reader.Read()
Dim ListItem as new ListItem
ListItem.Text = reader(0)
ListItem.Value = reader(1)
dProdCodeSearch.Items.Add(ListItem)
End While
it usually works for me..
Try changing your code as follows, I assume you are using ASP.NET
Dim ProductList As New ArrayList()
Dim ProdCodeSearch As String
Dim InstrumentSearch As String
pcSQL = " select distinct instrument_name AS [instrument_name], product_code AS [product_code] from FRUD.tblXref order by instrument_name "
Dim DBConn As SqlConnection
DBConn = New SqlConnection(ConfigurationManager.AppSettings("AMDMetricsDevConnectionString"))
DBConn.Open()
Dim reader As SqlDataReader
Dim DBCommand As New SqlCommand(pcSQL, DBConn)
reader = DBCommand.ExecuteReader()
While reader.Read()
dProdCodeSearch.Items.Add(reader(0))
End While
dProdCodeSearch.DataTextField = "instrument_name"
dProdCodeSearch.DataValueField = "product_code"
reader.Close()
精彩评论