Click here to Skip to main content
12,450,981 members (23,816 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET
My sample code:

string str = "";
 
foreach(ListItem lstIetm In MyListBox.Items)// through an exception here "unable to cast object of type 'System.String' to 'System.Web.UI.WebControls.ListItem'" 

{ 
  If(lstIetm.Selected) 
  { 
    str+=lstItem.Value.ToString()+","; 
  } 
} 
if(str!="") 
{ 
    str.Remove(str.LastIndexOf(',')); 
} 
 
Select * from <tablename> where Id in (str)

please suggest me how to resolve this exception and why it occurs. i go through all the resources. but i got the same foreach loop.

[edit]code block added[/edit]
Posted 16-Nov-12 19:09pm
Updated 17-Nov-12 1:57am
Nelek97.8K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

if (ListBox1.Items.Count > 0)
        {
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                    string selectedItem = ListBox1.Items[i].Text;
                    //insert command
                }
            }
        }
Do you want this?
or
public string GetListBoxSelStringInComma(ListBox Listbox1)
{
string selectedItem = "";
if (Listbox1.Items.Count > 0)
{
for (int i = 0; i < Listbox1.Items.Count; i++)
{
if (Listbox1.Items[i].Selected)
{
if (selectedItem == "")
selectedItem = Listbox1.Items[i].Text;
else
selectedItem += "," + Listbox1.Items[i].Text;
}
}
}
return selectedItem;
}
and for all records
protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            int _count = lstEmployees.Items.Count;
            if (_count != 0)
            {
                for (int i = 0; i < _count; i++)
                {
                    ListItem item = new ListItem();
                    item.Text = lstEmployees.Items[i].Text;
                    item.Value = lstEmployees.Items[i].Value;
                    lstSelectedEmployees.Items.Add(item);
                }
            }
            lstEmployees.Items.Clear();
        }
  Permalink  
v3
Comments
Member 8172875 17-Nov-12 2:09am
   
i am using sample code 2 and get this Error 1 'object' does not contain a definition for 'Selected' and no extension method 'Selected' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) C:\Users\admin\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\PrintSalarySlip.cs 184 47 WindowsFormsApplication1
Member 8172875 17-Nov-12 2:14am
   
actually i want a string of multiple selected values from listbox. and use this string into where clause for IN() statement. it will fetch all records which are selected by user in listbox and display records on gridview.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi

I think the above code you tried with "Selected" property is for web application and you are trying with windows application.

Can you try the below code:

using System.Collections.Generic;
 
int itemCount = listBox1.SelectedItems.Count;
List<string> lstItems = new List<string>();
 
for (int i = 0; i < itemCount; i++)
{
    lstItems.Add(listBox1.SelectedItems[i].ToString());
}
//string myInQuery = string.Join(",",lstItems);
 string myInQuery = string.Empty;
 for (int i = 0; i < lstItems.Count; i++)
 {
    if (myInQuery != string.Empty)
    {
        myInQuery = string.Format("{0},{1}",myInQuery, lstItems[i]);
    }
    else
       myInQuery = lstItems[i];
 }
  Permalink  
v4
Comments
Dominic Abraham 18-Nov-12 1:29am
   
If the solution is helpful, please mark it as answer.
Member 8172875 18-Nov-12 23:38pm
   
Error 1: The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments

Error 2: Argument '2': cannot convert from 'System.Collections.Generic.List' to 'string[]'

I got two errors while trying your code.. you are correct. i am working on windows application.
Dominic Abraham 19-Nov-12 1:40am
   
can you try after adding the namespace System.Collections.Generic also if not done. I checked this only in .Net 4.0. And you may be checking in old .Net version.
Dominic Abraham 19-Nov-12 2:11am
   
Please check the improved solution.
Member 8172875 19-Nov-12 2:15am
   
thanks Dominic. i have done with some modification on your code and its working properly .. thanks for the help
Member 8172875 19-Nov-12 2:17am
   
int itemCount = listBox1.SelectedItems.Count;
string str="";

for (int i = 0; i < itemCount; i++)
{

str += listBox1.SelectedItems[i].ToString() + "','";

}
Dominic Abraham 19-Nov-12 3:21am
   
Good...Whenever the post is helpful, don't forget to mark it as answer. Then it will be helpful to others also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100