Solved

error with listbox selection

Posted on 2008-10-17
9
471 Views
Last Modified: 2013-12-17
I have a listbox on my webform  populated values from the database table. i have a List<string> variable contains some values. i need to get the strings from list<string> variable and select those strings in the Listbox.

Here's the code i tried, but it gives me a error as object reference not set to an instance of the object.

 foreach (string s in Theatres)
                   {
                       TheaterListbox.SelectedItem.Text = s.ToString();   ---Getting Error in this line
                   }


Please help me out in this. Thanks
0
Comment
Question by:gladstonesheeba
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22744318
is your listbox bound to a datasource? if so, it should be:
TheaterListbox.SelectedValue = s.ToString();  

if not, it should be something like:
TheaterListbox.SelectedItem = s.ToString();  
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22744412
What type of error do you have.

What you do is like you set the selected item for your listBox which only valid when: your listBox has already contain list of items, and you whan one of them to be selected.
So the code you gave is not quite right.

I think you will have to add them instead of set the selected property


 foreach (string s in Theatres)
  {
        myList.Items.Add(new ListItem(s, s));
}

Hope this helps
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22744416
if its a multiselection then you will need to do this
ListBox1.ClearSelection();
 

foreach (string s in Theatres)

{

    ListItem item = ListBox1.Items.FindByText(s);

    if (item != null) item.Selected = true;

}

Open in new window

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22744440
try this code

foreach (string s in Theatres)
                   {
                       TheaterListbox.Items.FindByText(s.ToString()).Selected = true;
                   }
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:gladstonesheeba
ID: 22744456
I tried what you mentioned Theaterlistbox.selecteditem=s.tostring(); I gave me two errors. please look in to this.

Error      5      Property or indexer 'System.Web.UI.WebControls.ListControl.SelectedItem' cannot be assigned to -- it is read only      

Error      6      Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.ListItem'      
0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
ID: 22744490
If you want to select certain value of the list, then

if (myListBox.Items.FindByValue(s) != null)
       myListBox.Items.FindByValue(s).Selected = true;
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22744514
Have you try my 2nd post, the added solution? Is that you want to add these List<string> values to your ListBox
0
 

Author Closing Comment

by:gladstonesheeba
ID: 31507264
Excellent, Thanks for your help
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22744543
gladstonesheeba, unless you first try the examples we have provided, there is no point to discus further.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now