Linq Boolean returns exception DROPDOWNLIST has a SelectedValue which is invalid because it does not exist in the list of item

只谈情不闲聊 提交于 2020-01-24 13:12:08

问题


I have a dropdownlist that is bound to a linq data source. This drop down list shows all the bowzer numbers except those whos status is set to false in the database.
Suppose I have a record which was created earlier and now i want to edit the bowzer that is now set to false. I am thrown with this exception and i don't know how to deal with it.

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

Here is the code for my LinqDataSource

<asp:LinqDataSource ID="LinqBowzerDS" runat="server" ContextTypeName="ShippingWeb.ShippingDbDataContext"
EntityTypeName="" Select="new (bowzer_id, bowzer_no)" TableName="Bowzers" 
OrderBy="bowzer_no"  Where="expiry_date &gt;= DateTime.Now && status==True">

As soon as I click on EDIT button of the detailsView, where my dropdownlist is, I am given an exception that i've mentioned earlier. Is there any way i can catch this exception? and upon edit, show only those records who's status is True?

Here is the code for my detailsView

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4"
            DataSourceID="LinqDataSource1" Height="50px"

            Width="363px" 
            DataKeyNames="challan_id" ForeColor="#333333" GridLines="None" 
            ondatabound="DetailsView1_DataBound" onmodechanged="DetailsView1_ModeChanged" 
         >   

    <asp:TemplateField HeaderText="Bowzer No">
        <EditItemTemplate>
            <asp:DropDownList ID ="bowzerddl" runat="server"
            DataSourceID="LinqBowzerDS"
            DataTextField="bowzer_no"
            DataValueField="bowzer_id"
            selectedValue='<%#  bind("bowzer_id") %>'
            AppendDataBoundItems="true"
            >
            <asp:ListItem Selected="True" Value="">(None)</asp:ListItem>
            </asp:DropDownList>

        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblbowzer" runat="server" Text='<%# Eval("Bowzer.Bowzer_no") %>'>                                                                                                                                      </asp:Label>
        </ItemTemplate>

    </asp:TemplateField>

    <asp:CommandField ShowEditButton="True" />

</Fields>
<FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>

回答1:


I solved it from this link Answer No 16

This worked for us:

    protected void PreventErrorOnbinding(object sender, EventArgs e)
     {
         dropdownlist  theDropDownList = (DropDownList)sender;
         theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding);
         theDropDownList.AppendDataBoundItems = true;
         ListItem li = new ListItem("Make a selection >>","");
         theDropDownList.Items.Insert(0, li);
         try
         {
             theDropDownList.DataBind();
         }
         catch (ArgumentOutOfRangeException)
         {
             theDropDownList.SelectedValue = "";
         }
     }

On the control(s) you add ondatabinding="PreventErrorOnbinding"

Since the selected value is set to "" it always selects the firts item in the dropdownlist.



来源:https://stackoverflow.com/questions/13392040/linq-boolean-returns-exception-dropdownlist-has-a-selectedvalue-which-is-invalid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!