Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hye..

I have a dropdownlist which contains a customer record. I am facing a problem where although user select any item in the dropdownlist, the selectedvalue retrieved is the last item in the dropdown.

Here is my piece of code to view the data in the dropdownlist.

VB
Dim sqlCust As String = "SELECT CUSTCODE_T, CUSTNAME_T FROM CUST_MASTER"
         Dim dt As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlCust, DBLayer.ClsConnection.getConnString("MP_QFMS"))

         Me.ddlCust.Items.Add("--Select--")
         For Each dr As DataRow In dt.Rows
             Me.ddlCust.Items.Add(dr("CUSTNAME_T"))
             Me.ddlCust.DataValueField = dr("CUSTCODE_T")

         Next
         ddlCust.DataBind()


and this is the code to display the selected value into label

VB
Me.lblCust.Text = Me.ddlCust.DataValueField 


Based on the example below, user select SEP, but the selectedvalue retrieved is 03
CUSTCODE_T ; CUSTNAME_T
----------- -----------
01 ; SEP
02 ; OTH
03 ; EPSL
Posted

I think you want to do something like this instead (assuming you don't have viewstate turned off):

VB
If Not Page.IsPostBack Then
    Dim sqlCust As String = "SELECT CUSTCODE_T, CUSTNAME_T FROM CUST_MASTER"
     Dim dt As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlCust, DBLayer.ClsConnection.getConnString("MP_QFMS"))

     Me.ddlCust.Items.Add("--Select--")
     For Each dr As DataRow In dt.Rows
         Me.ddlCust.Items.Add(new ListItem(dr("CUSTNAME_T"), dr("CUSTCODE_T))
     Next

     'No need to DataBind() since you're adding the items manually
     'Also lose the DataValueField, since it's used in DataBind, which you're not using
End If


OR:

VB
If Not Page.IsPostBack Then
    Me.ddlCust.DataTextField = "CUSTNAME_T"
    Me.ddlCust.DataValueField = "CUSTCODE_T"

    Dim sqlCust As String = "SELECT CUSTCODE_T, CUSTNAME_T FROM CUST_MASTER"
     Dim dt As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable(sqlCust, DBLayer.ClsConnection.getConnString("MP_QFMS"))

     Me.ddlCust.Items.Clear()
     Me.ddlCust.Items.Add("--Select--")
     Me.ddlCust.AppendDataBoundItems = True        'This keeps the item added above when you do DataBind below

     Me.ddlCust.DataSource = dt
     Me.ddlCust.DataBind()
End If
 
Share this answer
 
Comments
snamyna 16-May-12 0:39am    
And to retrieve the data, I should use ddlCust.selectedvalue right?
Thanks so much Steve! Both codes work for me. :) :) :) :)
Steve Echols 16-May-12 0:42am    
Correct. That will get you the CUSTCODE_T value.

You're welcome!
Hi,

I suggest you to not use foreach loop to bind the dropdown.
While binding data to the dropdown you should use this:
VB
Me.ddlCust.DataSource=dt
Me.ddlCust.DataValueField="CUSTNAME_T"
Me.ddlCust.DataValueField = "CUSTCODE_T"
Me.ddlCust.Items.Insert(0, New ListItem("Select", String.Empty))


This may help you..

-AK
 
Share this answer
 
Comments
snamyna 16-May-12 0:40am    
Thanks for ur response Amit Kmr Sinha. :)
[no name] 16-May-12 1:47am    
Most Welcome.. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900