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("S ELECT * 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.FindByVal ue(objCoun tryID).Sel ected = True
End Sub
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("S
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
ddlCountry.Items.FindByVal
End Sub
You may need to use ddlCountry.Items.Clear() before each DataRead.
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.FindByVal ue(objCoun tryID).Sel ected = True
End Sub
Protected Sub cmdUseThis_Click(ByVal sender As Object, ByVal e As System.EventArgs)
ddlCountry.Items.Clear()
Dim objCountryID As Object = DataAccess.getScalarResult
ddlCountry.Items.FindByVal
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.