DropDownList has SelectedValue which in invalid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett

    #1

    DropDownList has SelectedValue which in invalid

    I have a DropDownList in an ASP.NET web form that is populated with items
    from a lookup table by binding that DropDownList to a SqlDataSource.
    However, the items in the lookup table can change over time. The problem is
    that when an item has been removed from the lookup table, and a user wants
    to retrieve a record that used the deleted item, the following error occurs:



    'ddlAssignedTo' has a SelectedValue which is invalid because it does not
    exist in the list of items.



    I need to allow the lookup table items to change, while still allowing
    retrieval of old records that used the now deleted items. I could add a
    field to the lookup table indicating whether an item is active or inactive,
    but only the active ones (and the selected one) should appear in the
    DropDownList.



    So, is there a way to either

    a) Allow entries into a DropDownList that are not in the databound table
    (like a VB ComboBox), or

    b) Add the missing value to the DropDownList items after it is bound to
    the table?





    Thanks,

    Brett


  • Allen Chen [MSFT]

    #2
    RE: DropDownList has SelectedValue which in invalid

    Hi Brett,

    From your description you want to avoid getting the exception:
    'ddlAssignedTo' has a SelectedValue which is invalid because it does not
    exist in the list of items.

    Is my understanding correct?

    If so you don't have to add an extra field in the data base. This exception
    is generally caused by binding the DropDownList on every postback. To
    demonstrate how this exception will be thrown I've created a demo:

    aspx.cs
    public partial class _Default : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {
    //Uncomment following code to reproduce this exception
    // this.DropDownLi st1.DataBind();
    }

    protected void Button1_Click(o bject sender, EventArgs e)
    {
    this.DropDownLi st1.SelectedVal ue = this.TextBox1.T ext;
    }
    }
    aspx:
    <asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
    ConnectionStrin g="<%$ ConnectionStrin gs:AConnectionS tring %>"
    SelectCommand=" SELECT * FROM [Table_1]"></asp:SqlDataSour ce>

    <asp:DropDownLi st ID="DropDownLis t1" runat="server"
    DataSourceID="S qlDataSource1" DataTextField=" theID"
    DataValueField= "theID">
    </asp:DropDownLis t>

    <asp:Button ID="Button1" runat="server" onclick="Button 1_Click"
    style="height: 26px" Text="Button" />
    <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>

    To reproduce please try following steps:
    1. View the page and enter an item of the DropDownList in the TextBox.
    2. Delete the record in the data base where the above item resides.
    3. Click the button to post back.

    Initially there's no exception. If you uncomment the code in the Page_Load
    event handler the exception will be thrown.

    So if we want to eliminate this exception, we should check if we bind the
    DropDownList on every postback. If it's done in code behind, following code
    may always help:

    if(!IsPostBack)
    {
    //Bind DropDownList
    }

    If binding the DropDownList on every postback is a must in the requirement
    I think we can do some check before setting the selected value of the
    DropDownList.

    As to your following concerns:
    =============== =============== =============== =====
    So, is there a way to either

    a) Allow entries into a DropDownList that are not in the databound table
    (like a VB ComboBox), or

    b) Add the missing value to the DropDownList items after it is bound to
    the table?
    =============== =============== =============== =====

    We can add an item in the PreRender event handler of the DropDownList
    control. Something like:
    void DropDownList1_P reRender(object sender, EventArgs e)
    {
    DropDownList ddl = (DropDownList)s ender;
    ddl.Items.Inser t(0, new ListItem("hello ", "world"));
    }

    If this problem isn't got solved please send me a demo project that can
    help me understand your scenario better.

    Regards,
    Allen Chen
    Microsoft Online Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subs...#notifications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://support.microsoft.com/select/...tance&ln=en-us.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    | From: "Brett" <brettlf@newsgr oup.nospam>
    | Subject: DropDownList has SelectedValue which in invalid
    | Date: Fri, 19 Sep 2008 17:42:02 -0400
    | Lines: 38
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
    | X-RFC2646: Format=Flowed; Original
    | Message-ID: <upNWsEqGJHA.35 04@TK2MSFTNGP02 .phx.gbl>
    | Newsgroups: microsoft.publi c.dotnet.framew ork.aspnet
    | NNTP-Posting-Host: dsl-1-210.d01.scpnbh. pbtcomm.net 64.53.27.210
    | Path: TK2MSFTNGHUB02. phx.gbl!TK2MSFT NGP01.phx.gbl!T K2MSFTNGP02.phx .gbl
    | Xref: TK2MSFTNGHUB02. phx.gbl
    microsoft.publi c.dotnet.framew ork.aspnet:7641 7
    | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
    |
    | I have a DropDownList in an ASP.NET web form that is populated with items
    | from a lookup table by binding that DropDownList to a SqlDataSource.
    | However, the items in the lookup table can change over time. The problem
    is
    | that when an item has been removed from the lookup table, and a user
    wants
    | to retrieve a record that used the deleted item, the following error
    occurs:
    |
    |
    |
    | 'ddlAssignedTo' has a SelectedValue which is invalid because it does not
    | exist in the list of items.
    |
    |
    |
    | I need to allow the lookup table items to change, while still allowing
    | retrieval of old records that used the now deleted items. I could add a
    | field to the lookup table indicating whether an item is active or
    inactive,
    | but only the active ones (and the selected one) should appear in the
    | DropDownList.
    |
    |
    |
    | So, is there a way to either
    |
    | a) Allow entries into a DropDownList that are not in the databound
    table
    | (like a VB ComboBox), or
    |
    | b) Add the missing value to the DropDownList items after it is bound
    to
    | the table?
    |
    |
    |
    |
    |
    | Thanks,
    |
    | Brett
    |
    |
    |

    Comment

    Working...