locked
Error message when Bind ASP.Net DropDownList RRS feed

  • Question

  • User-1559458579 posted

    I am working with an ASP.Net Web Forms application. I want to provide a user with a dropdownlist of values ****IMPORTANT****INSIDE A FORMVIEW***. The values are coming from a database table. When the page loads I get the following error.

    'DropDownList1' has a SelectedValue 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: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    This issue has dogged me many times and I've tried to avoid it but I would be using dropdown list like this often. I hope someone can help me solve it. I've providing the code below. IT IS IMPORTANT TO KNOW THIS IS INSIDE A FORMVIEW AND I BELIEVE YOU HAVE TO ACTUALLY FIND THE CONTROL INSIDE THE FORMVIEW. IF YOU CAN PLEASE PLEASE PROVIDE THE FULL AND COMPLETE SOLUTION. THAT IS THE ONLY WAY YOUR SOLUTION WILL HELP ME.

               

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="echodevelopment.WebForm3" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>form
            </div>
            <div>
                <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="id" DataSourceID="SqlDataSource1">
                    <EditItemTemplate>
                        id:
                        <asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
                        <br />
                        City:
                        <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
                        <br />
                        Citycode:
                        <asp:DropDownList ID="DropDownList1" runat="server" Text='<%# Bind("Citycode") %>' AppendDataBoundItems="True">
                            <asp:ListItem Value="0" Selected="True">Select</asp:ListItem>
                            <asp:ListItem Value="AAT">Atlanta</asp:ListItem>
                            <asp:ListItem Value="MPH">Memphis</asp:ListItem>
                            <asp:ListItem Value="CLV">Clevand</asp:ListItem>
                        </asp:DropDownList>
                        <%--<asp:TextBox ID="CitycodeTextBox" runat="server" Text='<%# Bind("Citycode") %>'/>--%>
                        <br />
                        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                        &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        City:
                        <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />
                        <br />
                        Citycode:
                        <asp:TextBox ID="CitycodeTextBox" runat="server" Text='<%# Bind("Citycode") %>' />
                        <br />
                        <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
                        &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </InsertItemTemplate>
                    <ItemTemplate>
                        id:
                        <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
                        <br />
                        City:
                        <asp:Label ID="CityLabel" runat="server" Text='<%# Bind("City") %>' />
                        <br />
                        Citycode:
                        <asp:Label ID="CitycodeLabel" runat="server" Text='<%# Bind("Citycode") %>' />
                        <br />
                        <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
                        &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                        &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
                    </ItemTemplate>
                </asp:FormView>
            </div>
            <div>data sources
            </div>
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EchoConnectionString %>" DeleteCommand="DELETE FROM [Test_city] WHERE [id] = @id" InsertCommand="INSERT INTO [Test_city] ([City], [Citycode]) VALUES (@City, @Citycode)" SelectCommand="SELECT [id], [City], [Citycode] FROM [Test_city]" UpdateCommand="UPDATE [Test_city] SET [City] = @City, [Citycode] = @Citycode WHERE [id] = @id">
                    <DeleteParameters>
                        <asp:Parameter Name="id" Type="Int32" />
                    </DeleteParameters>
                    <InsertParameters>
                        <asp:Parameter Name="City" Type="String" />
                        <asp:Parameter Name="Citycode" Type="String" />
                    </InsertParameters>
                    <UpdateParameters>
                        <asp:Parameter Name="City" Type="String" />
                        <asp:Parameter Name="Citycode" Type="String" />
                        <asp:Parameter Name="id" Type="Int32" />
                    </UpdateParameters>
                </asp:SqlDataSource>
            </div>
            <div>Code Datasource</div>
            <div>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:EchoConnectionString %>" SelectCommand="SELECT [Atlanta],

     FROM [Test_city_codes]"></asp:SqlDataSource>
            </div>
        </form>
    </body>
    </html>




    Friday, October 9, 2020 12:26 PM

All replies

  • User-943250815 posted

    The community can help better help if you provide some code.
    About exception you have and looking just to code provided, means, this dropdown does not have a value equal to one of itens in the list.

    Friday, October 9, 2020 1:07 PM
  • User-1559458579 posted

    I provided the code for the DropDownList. There is no more code that would be needed.

    Friday, October 9, 2020 1:16 PM
  • User-943250815 posted

    Seems after edit the dropdown mentioned earlier it not there anymore.
    Anyway, considering actual code provided. You have this dropdown

    <asp:DropDownList ID="DropDownList1" runat="server" Text='<%# Bind("Citycode") %>' AppendDataBoundItems="True">
                            <asp:ListItem Value="0" Selected="True">Select</asp:ListItem>
                            <asp:ListItem Value="AAT">Atlanta</asp:ListItem>
                            <asp:ListItem Value="MPH">Memphis</asp:ListItem>
                            <asp:ListItem Value="CLV">Clevand</asp:ListItem>
                        </asp:DropDownList>

    Now, consider value for Citycode = Phoenix, as you can see there is no ListItem for "Phoenix", in such case there will be an exception like you have now.
    Sometimes this happen because field on DB is "null", or even you have a value on DB that is not on ListItem.

    Friday, October 9, 2020 2:39 PM
  • User-1559458579 posted

    @jzero, based on what you said, each item that I have in the ListItems much match identical to the items coming from the database correct? So, Ih I have one items coming from the Bind value and that value is Atlanta, that same value must be in the ListItems? Is that correct? I have literally spend 5 days on this. I have been able to do everything else in developing the application except this. 

    So, it must be a 1 to 1 match correct?

    Friday, October 9, 2020 4:21 PM
  • User-943250815 posted

    @jzero, based on what you said, each item that I have in the ListItems much match identical to the items coming from the database correct?

    Yes, correct

    I suspect you made a try to set a datasource for your dropdownlist.
    If your logic permit, you can get all Cities (Code and Name) from DB and use it as Datasource for DropDown, with AppendDataBoundItens = True, you can set a ListItem like "Select" followed by each item from Datasource.
    This way all itens will be generated
    Check here http://www.tutorialspanel.com/bind-dropdownlist-control-using-sql-server-database/

    Friday, October 9, 2020 4:36 PM
  • User-1559458579 posted

    The strange thing about it @jzero, is the error happens on the binding. But I have to bind it to a database filed because the dropdown list is what will update the database field value. I have to do this ------> Text='<%# Bind("Citycode") %>'

    The list is actually short and I don't mind manually creating the list. But, even when I remove data source from the dropdown list and use a manual item list I still get the same error. Maybe I'm not supposed to use Bind! I'll try Eval. But this is the issue ----Text='<%# Bind("Citycode") %>'

    Friday, October 9, 2020 4:55 PM
  • User-943250815 posted

    Oops I missed this part in your code. There is no property named "Text" for Dropdown check declarative Syntax at
    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.dropdownlist?view=netframework-4.8

    Replace Text='<%# Bind("Citycode") %>' by Selectedvalue='<%# Bind("Citycode") %>'

    Friday, October 9, 2020 5:47 PM
  • User409696431 posted

    Let me add another note.  As written, you are not getting any dropdown items from a database, so AppendDataBoundItems="true" is not needed and is doing nothing.  That would be if you had a datasource for the DropDownList, were populating the dropdownlist items from it, and wanted additional items to appear before those items from the database (Such as a "SELECT").

    Friday, October 9, 2020 6:04 PM
  • User1535942433 posted

    Hi dmartinm,

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

    Accrding to your description,as far as I think,you could press F12 to select element.Expand this element, to see the option elements.The error you're seeing suggests that when you click on one of the rows, its value isn't matching one of the values listed in the options.

    You could find the solution for  the trailing spaces problem.And you could use just like:

    <asp:DropDownList ID="DropDownList1" runat="server"  SelectedValue='<%# Bind("Citycode")%>'> 

    Best regards,

    Yijing Sun

    Monday, October 12, 2020 5:35 AM