Solved

error with listbox selection

Posted on 2008-10-17
9
486 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

856 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