I have a drop down list that is populated from a database table:
<asp:DropDownList ID="userNameDropDown" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Name" DataValueField="PK_User" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [Name], [PK_User] FROM [User] ORDER BY [Name]">
</asp:SqlDataSource>
When I assign a specific row as default it works fine:
userNameDropDown.SelectedIndex = 3;
However when I use this dropdown:
<asp:DropDownList ID="catagoryDropDownListEdit" runat="server" DataSourceID="SqlDataSource5"
DataTextField="Catagory" DataValueField="PK_SupportCatagory">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [PK_SupportCatagory], [Catagory] FROM [SupportCatagory]">
</asp:SqlDataSource>
And try:
catagoryDropDownListEdit.SelectedIndex = 1;
I get this error:
'catagoryDropDownListEdit' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: 'catagoryDropDownListEdit' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value
Here is a pic that shows there are infact values: