Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Cannot access a closed file error on adding a file to a library

$
0
0

I am trying to copy the attachments from a list to a document library. When I try to write into a document library, occasionally I am getting following error "The remote server returned an error: (500) Internal Server Error.  Cannot access a closed file.  Cannot access a closed file.  "

Any ideas on what is causing the problem. The code is specified below, The error message is specified after the code. Can you please let me know if I am missing anything. I cant find anything wrong in the code or in the library. The code is running as WCF Service and the call to process this is made from Infopath form.

try { SPSecurity.RunWithElevatedPrivileges(delegate() { string siteURL = "Site URL"; using (SPSite site = new SPSite(siteURL)) { SPWeb oWeb = site.OpenWeb(); SPList oList = oWeb.Lists["List Name"]; SPList docDestination = oWeb.Lists["Destination Library on same site"]; SPFolder fldRoot = oWeb.Folders["Destination Library on same site"]; SPFileCollection flColl = null; SPQuery oQuery = new SPQuery(); oQuery.Query = "<Where><Eq>" + "<FieldRef Name=\"ID\"/><Value Type=\"Number\">" + RequestID + "</Value></Eq></Where>"; SPListItemCollection oItems = oList.GetItems(oQuery); foreach (SPListItem lstItem in oItems) { if (lstItem["ID"].ToString() == RequestID) { if (lstItem.Attachments != null && lstItem.Attachments.Count > 0) { foreach (String strName in lstItem.Attachments) { flColl = fldRoot.Files; SPListItem listtem = docDestination.Items.Add(); SPFile FileCopy = lstItem.ParentList.ParentWeb.GetFile(lstItem.Attachments.UrlPrefix + strName); SPQuery oQueryDest = new SPQuery(); oQueryDest.Query = "<Where><Eq>" + "<FieldRef Name=\"Name\"/><Value Type=\"Text\">" + FileCopy.Name + "</Value></Eq></Where>"; SPListItemCollection collListItems = docDestination.GetItems(oQuery); string destFile = flColl.Folder.Url + "/" + FileCopy.Name ; if (collListItems.Count > 0) { String strFileNamePrfix = FileCopy.Name.Substring(0, FileCopy.Name.Length - 3); strFileNamePrfix = strFileNamePrfix + "_" + collListItems.Count.ToString(); strFileNamePrfix = strFileNamePrfix + FileCopy.Name.Substring(FileCopy.Name.Length - 3,3); destFile = flColl.Folder.Url + "/" + strFileNamePrfix; } byte[] fileData = FileCopy.OpenBinary(); Trace.WriteLine("Board Services dest file" + destFile + " Count = " + collListItems.Count.ToString()); SPFile flAdded = flColl.Add(destFile, fileData,true); SPListItem item = flAdded.Item; flAdded.Item.Update();

} } } } } }); } catch (Exception ex) { string strLogEntry = ex.Message.ToString() + ex.StackTrace.ToString(); } finally {

}


Form submission failed. (User: xxxxx, Form Name: Template, IP: , Request: http://Site Url/Lists/List name/Task/editifs.aspx?List=e6647e38-cb01-46b3-8bee-308357045532&ID=116&Source=http://Site URL/Lists/List Name/AllItems.aspx?View={CB9F3C2B-49DF-4D75-9720-1E7E000FD229}&FilterField1=Status&FilterValue1=Completed&IsDlg=1&Web=706d51c9-e509-40f3-884a-7d40e126ff14, Form ID: urn:schemas-microsoft-com:office:infopath:list:-AutoGen-2013-12-06T16:11:54:747Z, Type: DataAdapterException, Exception Message: The remote server returned an error: (500) Internal Server Error.  Cannot access a closed file.  Cannot access a closed file.  
 at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)    
The remote server returned an error: (500) Internal Server Error.)



Viewing all articles
Browse latest Browse all 11571

Trending Articles