Solved

error with listbox selection

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

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

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

15 Experts available now in Live!

Get 1:1 Help Now