Click here to Skip to main content
15,889,773 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
In my application having Asp.net Listbox Control.
and this is content page.suppose i am using form it will work fine.but using content page the list box is not work.
now i used in my code:
dim getitem as string=listbox1.selecteditem.tostring().
but i have got error as Object reference not set to an instance of an object.
how can i solve this problem...
Posted
Updated 19-Feb-12 18:21pm
v2
Comments
Varun Sareen 17-Feb-12 7:03am    
Please post some code. The listbox is not being filled i think
Ed Nutting 17-Feb-12 7:30am    
Agreed - impossible to answer without code as it could be Varun's answer or it could be the user didn't select anything or it's the infamous SelectedItem isn't reliable where as SelectedIndex is or it's because he didn't give a value to the selected item or..or...or...
kishore sharma 20-Feb-12 0:41am    
Hi, IT Depends how you are bindint listbox, its no where related to content page& master page.You are using property as selecteditem (it will give selected text & selected value) so you cant take as string.
you can take as dim lstItem as listitem=listbox1.selecteditem.
(check once where you are bindng are you binding withour if not page.ispostback)

please post clear.

thanks
Masila.R 25-Feb-12 0:28am    
thanks.i found my mistake with ur help..
s i have bind without if not page.ispostback..
now it s working.
thanks again...

1 solution

Let's assume listbox1 is not null. So, the problem happens because none of the list box items is selected; in this case listbox1.SelectedItem is null; you are trying to dereference null reference by using ToString, which gives you this exception.

Please always run your code under debugger before asking questions like this one. If you did, you would see the problem immediately. Generally, always use the debugger every time you have a slightest concern about your run-time behavior.

[EDIT]

Please see my past answer where I complied several past answers on related problems, with source code samples applicable to ComboBox and ListBox:
combobox.selectedvalue shows {} in winform[^].

—SA
 
Share this answer
 
v2

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