Reputation: 126
I am getting a access problem when exporting a crystal report as a pdf stream. Below is the code which runs in a background task in a ASP.net MVC web application:
using (var memoryStream = new MemoryStream())
using (var stream = reportDoc.ExportToStream(ExportFormatType.PortableDocFormat))
{
stream.CopyTo(memoryStream);
data = memoryStream.ToArray();
}
Following is the stack trace of exception:
System.Runtime.InteropServices.COMException (0x80004005): The process cannot access the file because it is being used by another process.
at CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext)
at CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportOptions options)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportFormatType formatType)
How to fix this problem?
Upvotes: 2
Views: 4511
Reputation: 9469
Do you open report file by using reportDocument.Load method. If so try to use OpenReportMethod.OpenReportByTempCopy option, so it looks like this
reportDocument.Load(file, OpenReportMethod.OpenReportByTempCopy);
Upvotes: 1
Reputation: 3190
We "fixed" the problem in our ASP.NET MVC application by adding retry around ExportToStream. We tried all the other suggestions but nothing worked.
using (var memoryStream = new MemoryStream())
using (var stream = RetryHelper.Retry(() => reportDoc.ExportToStream(ExportFormatType.PortableDocFormat)))
{
stream.CopyTo(memoryStream);
fileInfo.Data = memoryStream.ToArray();
}
...where the Retry() method is...
public static T Retry<T>(Func<T> action)
{
bool success = false;
int count = 0;
T response = default(T);
while (!success)
{
try
{
count++;
response = action();
success = true;
}
catch (Exception ex)
{
//only try 10 times with increasing sleeps
if (count >= 10)
{
throw;
}
string message = ex.Message;
int sleepTimeInMilliSeconds = 100 * count * count;//100ms, 400, 900, 1600 ......
Log.Debug(string.Format("Failed. Attempt {0}. Error={1}. Sleeping for {2}ms", count, message, sleepTimeInMilliSeconds), ex);
Thread.Sleep(sleepTimeInMilliSeconds);
}
}
return response;
}
Rant - I'm not a big CR fan. We are in the process of moving from CR to a phantomjs based pdf solution.
Upvotes: 0
Reputation: 5798
Mainly the issue is your memory of crystal doc. So after each exporting, you have to clean your objects, clear the cache and GAC.
First check above link, I search and find one thoughs as below (click here to know more).
As usual with errors that only come up occasionally, it's hard to say. And 99% of the time, an SP will not resolve the issue. One thing you do not say is how you recover from this error? Restart the app? Reboot?
I'd start with looking into the %temp% folder to see if there are any orphaned CR temp files. If there are, you'll need to think about why these would be getting orphaned.
Look in the Event Viewer. Any errors? Warnings?
Make sure you're using .Close and .Dispose on the report objects when you get done with these.
If you're using datasets, do the same.
Does this happen an high load times? If so, you may want to investigate the licensing and performance limitations of Crystal Reports.
Upvotes: 2
Reputation: 34
Your database is open, in your project, go to the database view, click on the database, click on refresh, then click on close connection. After this you should be able to run this code.
Upvotes: 0