Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cleanup code after a call to Crystal Reports ExportToHttpResponse method?

We're having a problem in our ASP.Net application where the Crystal Reports engine leaves garbage .tmp files in the server's Temp folder after generating reports for the users.

So we're trying to figure out how to run the .Close() and .Dispose() methods on the Report object, but we're finding that the code never gets run after the export occurs.

MyReport.Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, 
        this.Response, true, "My_Report");

MyReport.Report.Close();
MyReport.Report.Dispose();

Breakpoints set on the last two lines never get hit, and we've also tried putting other code there to test the processing. None of it runs. (I've also seen this question on other sites with similar code, but no answers)

I assume the ExportToHttpResponse method is returning a file stream (PDF) to the user at that point, ending the processing so the rest of the code doesn't get run. If that's the case, how do we get CR to perform a cleanup on the temp files, which the Close() and Dispose() methods are supposed to do? Do we have to implement a manual after-the-fact cleanup?

like image 821
Raelshark Avatar asked Feb 11 '10 20:02

Raelshark


2 Answers

I don't have a way of reproducing this issue so I'll throw out there that you may be able to use the using statement which allows you to specify when objects should be released.

What is the C# Using block and why should I use it? http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx

Haven't tried this, but I think you might be able to do something like

using(MyReport m = new MyReport())
{
   m.Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, 
        this.Response, true, "My_Report");
}

As I'm typing it I'm not sure that it would be much different than what you have already, but oh well, it's something to try. In my head this works. :)

Hope it helps.

like image 75
Dusty Avatar answered Dec 07 '22 07:12

Dusty


I actually had this exact problem and found that you can force the Close() and Dispose() methods to run by enclosing the exporting method in a Try Catch block and placing the Close() and Dispose() methods in the Finally like this:

            Try
            MyReport.Report.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Page.Response, True, ReportName)
        Catch ex As System.Exception

        Finally

            MyReport.Report.Close()
            MyReport.Report.Dispose()

        End Try
like image 35
Jesif Avatar answered Dec 07 '22 06:12

Jesif