Solved

Populate DropDown by Clicking Button

Posted on 2008-10-21
3
193 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:patelajk
3 Comments
 
LVL 4

Expert Comment

by:justin-clarke
ID: 22766205
You may need to use ddlCountry.Items.Clear() before each DataRead.
0
 

Author Comment

by:patelajk
ID: 22766242
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

0
 
LVL 3

Accepted Solution

by:
Blackninja2007 earned 125 total points
ID: 22766302
You need to to check if the object exists before clearing it.

if not(ddlcountry is nothing) then
  ddlcountry.items.clear()
end if
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now