Link to home
Create AccountLog in
Avatar of pposton
ppostonFlag for United States of America

asked on

DropDownList Error in Edit Mode - Missing Value

I have a dropdownlist which populates from a sqldatasource.  Each list is built by a user and may be edited at any time (items added and/or deleted).  When a user goes to edit a record in GridView and the dropdownlist contains an item that has been deleted, the following error occurs (Selection out of range Parameter name: value).

Is there a way to basically have a missing value ignored?  I do not want to add the value back into the list as it was deleted on purpose to begin with.  My current code for the dropdownlist is below.

I've seen some solutions online, but nothing simple or that pertains to my situation.  Thanks for the help!

                <asp:DropDownList ID="giftCategoryDD" runat="server" DataSourceID="SqlDataSource2" DataTextField="GiftCategory" DataValueField="giftCategory" SelectedValue='<%# Bind("giftCategory")%>'></asp:DropDownList>

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

When a dropdownlist is bound, you can only select an item which is in the list so the error makes sense. What else would you want to do? Why would you delete a category if it is being used in other tables?
Avatar of pposton

ASKER

We deal with non-profits assisting those in need.  They can develop their own lists of services, such as: food, rent, utilities, etc.  Let's say they decide they don't want to use utilities any more but want to use electric, water, gas.  They add those and delete utilities.  However, if they go back to edit their records of assistance they can't change utilities to electric because an error is thrown due to utilities being missing.  So, I need to be able to basically ignore the error.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of pposton

ASKER

I'm pretty new at development so forgive some my questions that may seem elementary.  My table has 3 columns id(pk), agencyName & Category.  Are you saying if I use that "id" column for the ddl even though it may not be sequential (since some have been deleted), that would prevent the error?  

I guess my query would look something like "Select id, Category From giftCategory WHERE agencyName = 'SalvationArmy'"  Isn't there a way in the code behind to ignore the missing value by using onDataBinding?  I've seen something like that, but not exactly what I need.  Any code examples would be appreciated as well.

Thanks for taking the time!
It would involve two tables (or three if you handle the AgencyName in same way)

Category:
CategoryID
CategoryName

Gifts:
ID
AgencyName
CategoryID


Then you query would look like

Select ID, AgencyName, CategoryName as Category
From Gifts
Inner Join Category On Category.CategoryID=Gifts.CategoryID
Avatar of pposton

ASKER

Thanks for the help!!!