Handling Dropdownlist selectedvalue exception

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 15-Oct-2007 22:46:00   

This is not exactly an LLBLGen issue but I'm hoping that some people who use LLBLGen datasource controls might have a good idea on handling this.

Scenario: LLBLGen Pro v 2.5 (adapter), ASP.NET web application with a formview. The formview is bound to an LLBLGenDatasource2 control and the edit item template has a dropdownlist which is also bound to an LLBLGenDatasource2 control. The dropdownlist is using two-way databinding.


            <asp:DropDownList id="ddlCategory" runat="server"
        AppendDataBoundItems="true"
                    DataSourceID="dsCategory"
                    DataTextField="Category" 
                    DataValueField="CategoryId" 
                    SelectedValue='<%# Bind("CategoryId")%>'                                    >
                    <asp:ListItem Value="">--No Selection--</asp:ListItem>
                    </asp:DropDownList>

The datasource only returns categories marked as "Active" but the existing data contains all categories. When I attempt to edit a record with an inactive category, I get an exception "'ddlCategory' has a SelectedValue which is invalid because it does not exist in the list of items."

I'd like to know what people are doing to handle this kind of error in their applications.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 16-Oct-2007 10:59:01   

The datasource only returns categories marked as "Active" but the existing data contains all categories. When I attempt to edit a record with an inactive category, I get an exception "'ddlCategory' has a SelectedValue which is invalid because it does not exist in the list of items."

IMHO, the original inactive Category of that entity should be displayed in the dropdownList with all other active categories. You'll have to handle the dropdownList on DataBound and manually insert a ListItem with the ID and Name of the related inactive category.

jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 16-Oct-2007 16:28:31   

I was (and still am) hoping for a better answer. I already have two options but would like some other input. I can solve this problem by using stored procedures (faux-code below).


CREATE PROCEDURE   dbo.pr_SomeList
    (
    @ExistingValue int = -1
    )
AS

SELECT  Id, TextField, Active           
FROM   dbo.MyTable
WHERE    Active = 1 OR Id = @ExistingValue

That approach works very well but it means I have to create a stored proc for each list and use the MS SQLDataSource controls. (Or do more coding in .NET and use an ObjectDataSource control.)

My second option is to sub-class the dropdownlist control and catch the exception during the PerformDataBinding event. At that point, I can change the SelectedValue to a known existing fallback value.


protected override void PerformDataBinding(System.Collections.IEnumerable dataSource)
{
    try
    {
        base.PerformDataBinding(dataSource);
    }
    catch (ArgumentOutOfRangeException ex)
    {
        this.SelectedValue = this.DefaultItemValue;
        base.PerformDataBinding(dataSource);
    }

}



I'm looking for any other ideas that might be out there.

G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 18-Oct-2007 14:47:48   

I use the perform select events ...

I just retrieve the active categories. Then I have an entity collection containing these. The just check which category you have checked on your item and if that is not in this collection, that add that entity to your collection.

After that bind your collection to your dropdownlist

JuanV
User
Posts: 12
Joined: 08-Jul-2007
# Posted on: 29-Dec-2007 23:31:36   

Add

AppendDataBoundItems="True"

to your ASP tags of your dropdownlist and that will fix it.