Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DropDownList Databindings 1

Status
Not open for further replies.

JLizOB

Programmer
Dec 5, 2007
45
CA
I have a dropdownlist that use an objectdatasource as its datasource. On the databound event I am adding another list item to the dropdownlist with this statement:
Code:
DropDownList ddl = (DropDownList)sender;
ddl.Items.Insert(0, new ListItem("- select Purchasing Division", ""));
Now the dropdownlist is in a formview so I edited its databindings to a field being returned from a database. If the field has a value it works fine but if the field is null it errors out with the following message:
System.ArgumentOutOfRangeException: 'DropDownListPurchaseDivision' has a SelectedValue which is invalid because it does not exist in the list of items

Is there anyway to compensate for null values in this situation? Thanks!
 
Early databinding a ddl can lead to this situation, but i think i overcame it in the past,

try this, but dont quote me on it!

--add a 0 to the value of your new item (and capitalize the S)
ddl.Items.Insert(0, new ListItem("- Select Purchasing Division", "0"));

--adjust your sql statement if possible
SELECT FieldID, Field1, ISNULL(DDLField,0)
FROM myTableNameGoesHere
 
I tried that but no luck. I put a break point when the following code is being executed:
Code:
DropDownList ddl = (DropDownList)sender;
ddl.Items.Insert(0, new ListItem("- select Purchasing Division", ""));

And it is erroring out before the databound method gets called.
 
try inserting your item this way...

<asp:DropDownList id=myDDLName runat=server AppendDataBoundItems="True">
<asp:ListItem Text="Select Purchasing Division" Value="0" />
</asp:DropDownList>
 
Before reading this I ended up appending the select row in my bll and that got it working, but adamroof your way seems much cleaner so I switched to that and it worked too. Thanks for the help everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top