Solved

Populate DropDown by Clicking Button

Posted on 2008-10-21
3
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

636 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