Solved

error with listbox selection

Posted on 2008-10-17
9
488 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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