Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My sample code:

C#
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
Updated 17-Nov-12 1:57am
v2

C#
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
C#
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
C#
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();
        }
 
Share this answer
 
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.
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:

C#
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];
 }
 
Share this answer
 
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<string>' 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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900