Solved

error with listbox selection

Posted on 2008-10-17
9
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 13

Expert Comment

by:TechTiger007
ID: 22744440
try this code

foreach (string s in Theatres)
                   {
                       TheaterListbox.Items.FindByText(s.ToString()).Selected = true;
                   }
0
 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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