Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populate DropDown by Clicking Button

Posted on 2008-10-21
3
Medium Priority
?
246 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

963 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