SPListItem item.delete

Posted on 2011-04-22
Medium Priority
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
  • 2
  • 2
LVL 12

Accepted Solution

techExtreme earned 2000 total points
ID: 35456322
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

ID: 35456324
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?

Author Comment

ID: 35459688
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.

Author Comment

ID: 35460564
Thanks Dude!

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 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