SPListItem item.delete

Posted on 2011-04-22
Last Modified: 2012-06-27
I have a webpart that displays a document library list.  It has a checkbox that allows users to select certain documents and then transfer to another document library by clicking the submit button.  When they click Submit, it should delete it out of the current document library.

The delete does happen but it still shows in my webpart.  When I click refresh, I get the error below because the listitem does not exist.

Value does not fall within the expected range.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Value does not fall within the expected range.

Source Error:

Line 27:                 if (complete.Checked)
Line 28:                 {
***Line 29:                         SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value))); ***
Line 30:                         byte[] fileBytes = item.File.OpenBinary();
Line 31:                         string destUrl = destList.RootFolder.Url + "/

foreach (GridViewRow row in GridView1.Rows)
                HiddenField hdID = (HiddenField)row.FindControl("hdID");
                CheckBox complete = (CheckBox)row.FindControl("complete");
                DropDownList ddlStatus = (DropDownList)row.FindControl("status");
                TextBox txtCaseNumber = (TextBox)row.FindControl("caseNumber");
                DropDownList ddlPriority = (DropDownList)row.FindControl("priority");                              

                if (complete.Checked)
                        SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value))); 
                        byte[] fileBytes = item.File.OpenBinary();
                        string destUrl = destList.RootFolder.Url + "/" + item.File.Name;
                        SPFile destFile = destList.RootFolder.Files.Add(destUrl, fileBytes, true /*overwrite*/);

                        // add the metadata to File            
                        SPListItem destItem = destFile.Item;
                        destItem["STATUS"] = ddlStatus.SelectedValue.ToString();
                        destItem["CASE NUMBER"] = txtCaseNumber.Text.ToString();
                        destItem["PRIORITY"] = ddlPriority.SelectedValue.ToString();
                        destItem["COMPLETE"] = complete.Checked.ToString();


Open in new window

Question by:Isaac
    LVL 12

    Accepted Solution

    Dude! make sure you rebind your grid as soon as  you delete a item. The temporary information stored will be removed once you rebind and hopefully your error will disappear.
    LVL 12

    Expert Comment

    And to add to my point above, if you are dealing with files inside sharepoint document library, why not use SPFile.MoveTo   command to move the file directly instead of deleting it?
    LVL 5

    Author Comment

    I needed to move multiple selected documents and the way I found to do it was the code I have above.  How can I modify the above to use SPFile.MoveTo ?  It seems to be more efficient.
    LVL 5

    Author Comment

    Thanks Dude!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
    Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now