Solved

Populate DropDown by Clicking Button

Posted on 2008-10-21
3
210 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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