Question

I am developing a VB.NET ASPX file which is currently working. But now it errors cause I added this one new input parameter which is a dropdownlist. The dropdownlist works properly now. Its source comes from a SQL DataReader. But hte problem is that the fields that the user should see are different from underlying values. So I want to create a 2 dimensional array list or somehow create a mapping as follows: Visible Text Invisible Value AAAAA 601 BBBBB 602 CCCCC 603 ...

So the user should just see AAAAA, BBBBB, CCCCC, etc. as choices. But then if they choose "BBBBB", my SQL query must filter records on field = 602.

Here is excerpt of my code now:

        Dim pcSQL As String
        Dim ProductList As New ArrayList()
        Dim ProdCodeSearch As String
        Dim InstrumentSearch As String

        pcSQL = " select distinct instrument_name, product_code from FRUD.tblXref order by instrument_name "
        Dim DBConn As SqlConnection
        DBConn = New SqlConnection(ConfigurationManager.AppSettings("AMDMetricsConnectionString"))
        DBConn.Open()
        Dim reader As SqlDataReader
            Dim DBCommand As New SqlCommand(pcSQL, DBConn)       
        reader = DBCommand.ExecuteReader()

        While reader.Read()
        ProductList.Add(reader(0),reader(1))
        End While

        dProdCodeSearch.DataSource = ProductList(0)
        dProdCodeSearch.DataBind()
        reader.Close()

        ProdCodeSearch = dProdCodeSearch.SelectedValue
Was it helpful?

Solution

You have to set the DataTextField and the DataValueField of the DropDownList before you DataBind it.

dProdCodeSearch.DataSource = reader
dProdCodeSearch.DataTextField  = "instrument_name"
dProdCodeSearch.DataValueField = "product_code"     
dProdCodeSearch.DataBind()

The DataTextField is the column that you want the user to see and the DataValueField normally is the primary-key column.

You need the DropDownList's SelectedValue to get the identifier of the selected item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top