Hi all,

I have a bit of a problem with the selected value of a drop down box in asp.
i get this error:

'drpSponsor' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

my code for the binding is this:

<asp:DropDownList ID="drpSponsor" runat="server" DataSourceID="srcSponsorNames" DataTextField="Name"
DataValueField="Name" SelectedValue='<%# Bind("Sponsor") %>'>
</asp:DropDownList>


Any help would be greatly appreciated.

thanks
Deeee

um well look at the values in srcSponsorNames and im sure you will not see the value of Bind("Sponsor"). you are trying to select a value that does not exist

Thank you for your responce Plazmo, how else would i be able to retrieve a value that has been selected previously and is now stored in the table?

Regards,
Deeee

If your datasource is database table, i would suggest you to use code-behind file load event to set the selected value of drop-down list.

Private Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) handles MyBase.Load

If Not IsPostBack Then
drpSponsor.DataSource = DataSource
drpSponsor.DataTextField = "FieldName"
drpSponsor.DataValueField = "FieldValue"
drpSponsor.DataBind()
drpSponsor.Items.FindByText("Sponsor").Selected = True
End If

End Sub


hope this might help,

http://msdn2.microsoft.com/en-us/library/xy14hwwb(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist(VS.80).aspx

This article has been dead for over six months. Start a new discussion instead.