Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["MyList"];
    if (list != null)
    {
        for (int i = list.ItemCount - 1; i >= 0; i--)
        {
            list.Items[i].Delete();
        }
        list.Update();
    }


I have more Items in an sharepoint list then when i in an listbox select only one item and delete if with the following code , then it deletes al the items in my list , not just the only i have selected , can somebody help me ?
Posted

Because you delete all the items in your code!
You should try to match your criteria before delete it.

SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["MyList"];
    if (list != null)
    {
        for (int i = list.ItemCount - 1; i >= 0; i--)
        {
            if( list.Items[i].equals("your criteia ")){
               list.Items[i].Delete();
            }
        }
        list.Update();
    }
 
Share this answer
 
SQL
As you have a loop and in a loop you have put a code, which delete itmes so all items will deleted, if you want to delete only the selected element then don't take a loop and use this code,

 if (list != null)
{
     list.Items.Remove(list.SelectedItem);
        list.Update();
}
 
Share this answer
 

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