Answered by:
Has a SelectedValue which is invalid because it does not exist

Question
-
User774350475 posted
On my page, I have ObjectDataSource and ListView. In Oninit method, I am setting selected value of Listbox.
In above scenario, it throws error if I set SelectedValue property of ListBox and if that item does not exist in ListBox collection.
"has a SelectedValue which is invalid because it does not exist "
I want to suppress this error if that item to be have selected does not exist in collection.
I am thinking to hook DataBinding or Selected event of ObjectDataSource and suppress error in that event. Could you please give example of those events in this context?
--
Thanks,
Mahesh
Thursday, June 26, 2008 2:45 PM
Answers
-
User1564875471 posted
When ListBox is databound using ObjectDataSource, I think ListBox's Items collection would be nullNo it shouldn't be null, you need to to access the list \box in Page_PreRender and not page _load ,
so the mentioned code in my previous post must be used in Page_PreRender
As mentioned by the other member , you can use try /catch to handle that error, But you sdhouldn't rely on try/catch , because it will slows your application , instead you need to check if the value exists before its get selected .
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 27, 2008 5:09 AM
All replies
-
User1564875471 posted
Instead of using the SelectedValue ,
Dim li As ListItem = DropDownlist1.Items.FindByValue("Value") If li IsNot Nothing Then li.Selected = True End If
Thursday, June 26, 2008 3:35 PM -
User774350475 posted
When ListBox is databound using ObjectDataSource, I think ListBox's Items collection would be null.
Thursday, June 26, 2008 10:12 PM -
User848370396 posted
when you wnats to suppress error, then keep that logic in try catch block and leave without writing anything in catch block.
try
{
your statements
}
catch(Exception e)
{
}
Friday, June 27, 2008 12:57 AM -
User1564875471 posted
When ListBox is databound using ObjectDataSource, I think ListBox's Items collection would be nullNo it shouldn't be null, you need to to access the list \box in Page_PreRender and not page _load ,
so the mentioned code in my previous post must be used in Page_PreRender
As mentioned by the other member , you can use try /catch to handle that error, But you sdhouldn't rely on try/catch , because it will slows your application , instead you need to check if the value exists before its get selected .
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 27, 2008 5:09 AM