Click here to Skip to main content
15,898,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i having a listbox and button .when i click button if the listbox item got selected it should navigate to another page

i used this code
C#
var ls=listBox1.SelectedItem.ToString();
         if (ls=="")
         {
             MessageBox.Show("select salesmanname").ToString();

         }
         else
         {
             NavigationService.Navigate(new Uri("/customernames.xaml", UriKind.Relative));
         }

but i am getting null point exception
Posted
Comments
[no name] 31-May-14 7:50am    
Debug your code and find out where you are getting this "null point exception".

What are you trying to do with "MessageBox.Show("select salesmanname").ToString();"?
Nandhini Devi 31-May-14 7:56am    
near var ls=listboxitem...
Nandhini Devi 31-May-14 8:02am    
if user dnt select list then message box should be displayed
[no name] 31-May-14 8:15am    
Okay and what is the purpose of the ToString() there?
[no name] 31-May-14 8:16am    
Debug your code and find out exactly where you are getting that exception. Then you can fix it. "Near" is not good enough.

1 solution

Try this :

C#
var ls = listBox1.Text;


in place of

C#
var ls=listBox1.SelectedItem.ToString();


:):):)
 
Share this answer
 
Comments
Nandhini Devi 31-May-14 8:38am    
i am using it for windows phone and listbox1.text not coming
HardikPatel.SE 31-May-14 8:48am    
then try this :
ListBox1.SelectedValue.ToString();
HardikPatel.SE 31-May-14 8:49am    
listBox1.SelectedItem.ToString();

listBox1.SelectedValue.ToString();
Nandhini Devi 31-May-14 9:23am    
still error null reference exception
Nandhini Devi 1-Jun-14 1:19am    
any other option to erradicate this error ?

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