Link to home
Start Free TrialLog in
Avatar of patelajk
patelajk

asked on

Populate DropDown by Clicking Button

Hi

I am trying to select an item in a drop down by clicking a command button but I keep on getting an error saying "Cannot have multiple items in drop down"

The drop down does not have any duplicated so I am not sure why this is happening

Code is below.... Any ideas??

thanks


      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)            
            If Not Page.IsPostBack Then
            ddlCountry.DataSource = DataAccess.ReaderFromSP("SELECT * FROM tblCountry ORDER BY Country_Name ASC")
            ddlCountry.DataTextField = "Country_Name"
            ddlCountry.DataValueField = "Country_ID"
            
            ddlCountry.DataBind()
            End If
      End Sub

'-- button click event
      Protected Sub cmdUseThis_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim objCountryID As Object = DataAccess.getScalarResult("SELECT City_Country_ID FROM tblCity WHERE City_ID = " & listLocInfo.SelectedValue)

            ddlCountry.Items.FindByValue(objCountryID).Selected = True
            
      End Sub
Avatar of justin-clarke
justin-clarke

You may need to use ddlCountry.Items.Clear() before each DataRead.
Avatar of patelajk

ASKER

tried putting this at the top like below but i get a "Object reference not set to an instance of an object" error as the drop down is cleared:
     Protected Sub cmdUseThis_Click(ByVal sender As Object, ByVal e As System.EventArgs)
ddlCountry.Items.Clear()
Dim objCountryID As Object = DataAccess.getScalarResult("SELECT City_Country_ID FROM tblCity WHERE City_ID = " & listLocInfo.SelectedValue)

            ddlCountry.Items.FindByValue(objCountryID).Selected = True
           
      End Sub

ASKER CERTIFIED SOLUTION
Avatar of Blackninja2007
Blackninja2007

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