1

I have the following asp.net DropDownList:

<asp:DropDownList ID="ddlRole" runat="server" DataSourceID="dsRole"
        DataTextField="RoleCode" DataValueField="ID" Font-Names="Verdana" Font-Size="11px"
        ForeColor="#2D2D2D" ClientIDMode="Static" Width="120px" CssClass="selectedRole"
        OnDataBound="ddlEntity_DataBound" AppendDataBoundItems="true" >
        <asp:ListItem Value="-1">--- Select ---</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="dsRole" runat="server" SelectMethod="GetList" TypeName="Roles"
     OldValuesParameterFormatString="original_{0}"></asp:ObjectDataSource>

I get the "SelectedValue is Invalid Because it does not Exist in the List of Items" error, stating 'value' is incorrect. The ID I'm referencing is 1 and it definitely exists in the DB where I'm calling from.

I've tried solutions to this problem as cited on the internet and other forums, but I still get the same error. I'm assuming this has something to do with the fact that my SelectedValue starts off at -1?

Any suggestions?

1
  • 2
    When do you set the selected value? Is before databinding or after? Apr 25, 2012 at 10:35

1 Answer 1

-2

I decided to do so to "Select" was the first line in the transmitted data set. You can do it with UNION in MSSQL.

Request this has turned out like this:

SELECT '-1' AS id, 'Select' AS name UNION ALL SELECT id, name FROM users

Must give a column alias, or DropDownList simply does not understand where the id, and where the name

 <asp:DropDownList ID="MainCat" runat="server" AutoPostBack="True" 
         DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="id">
 </asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
         ConnectionString="<%$ ConnectionStrings:ConnectionString1%>" 
             SelectCommand="SELECT id,name FROM users">
 </asp:SqlDataSource>

I need to do the same suit

1
  • 1
    We shouldn't do this union, this should be handled by ASP .Net, not SQL. The error is from ASP .Net so ASP .Net should fix it. Apr 25, 2012 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.