Click here to Skip to main content
15,898,769 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
li_Make.Items.FindByText("Maruti Suzuki").Selected = true;

li_make is a listbox with Car makes binded on it...WHen i try to find a make in the above way i get the object reference not found error...error occurs only when the string passed to the findbytext function contains space or hyphen...how can i avoid this...thank you for your comments in advance
Posted
Updated 18-Jan-13 18:47pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Jan-13 2:18am    
Which ListBox? Do you think there is only one? :-)
Full type name, please. And tag your UI library or application type.
—SA
ram mohan 18-Jan-13 3:49am    
Do null check before selection is must.
Nandakishore G N 18-Jan-13 4:31am    
where are you placing that code?

1 solution

Hi,

You need to test if
C#
li_Make.Items.FindByText("Maruti Suzuki")
is null before using one of its properties.

If you use an object property and the object is null then it will throw an exception object reference not found.

To avoid throwing the exception you could do a test before using your object, for example in that way:

SQL
if (li_Make.Items.FindByText("Maruti Suzuki")!= null)
    li_Make.Items.FindByText("Maruti Suzuki").Selected = true;


you can see details about his exception here:
http://msdn.microsoft.com/en-us/library/system.data.objectnotfoundexception.aspx[^]

Valery.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900