I still do like Crystal reports but getting them to work on a production machine can be pretty frustrating. Responses to earlier posts showed me that I'm not the only one. So here's another.
You can print a Crystal report on a webpage by exporting it as a PDF or a Word doc. The browser will catch this in a viewer from where you do the real printing. There is a sample on the Crystal website which I followed quite sheepishly. It worked on my machine but the production machine showed a quite interesting error
C:\DOCUME~1\S01NT0~1\ASPNET\LOCALS~1\Temp\temp_b8805d74-11f7-4721-b614-88b564c0b8a4.rpt: Access to report file denied. Another program may be using it.
There is indeed an access problem, but not with the report file. Browsing the web I found the solution on the Crystal site. This led me to do some refactoring on the example which resulted in a helper method I would like to share with you:
protected void exportReport(CrystalDecisions.CrystalReports.Engine.ReportClass selectedReport, CrystalDecisions.Shared.ExportFormatType eft)
{
string contentType ="";
// Make sure asp.net has create and delete permissions in the directory
string tempFileName = System.Configuration.ConfigurationSettings.AppSettings["TempDir"] + Session.SessionID.ToString() + ".";
switch (eft)
{
case CrystalDecisions.Shared.ExportFormatType.PortableDocFormat :
tempFileName += "pdf";
contentType = "application/pdf";
break;
case CrystalDecisions.Shared.ExportFormatType.WordForWindows :
tempFileName+= "doc";
contentType = "application/msword";
break;
}
CrystalDecisions.Shared.DiskFileDestinationOptions dfo = new CrystalDecisions.Shared.DiskFileDestinationOptions();
dfo.DiskFileName = tempFileName;
CrystalDecisions.Shared.ExportOptions eo = selectedReport.ExportOptions;
eo.DestinationOptions = dfo;
eo.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
eo.ExportFormatType = eft;
selectedReport.Export();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = contentType;
Response.WriteFile(tempFileName);
Response.Flush();
Response.Close();
System.IO.File.Delete(tempFileName);
}
You pass the method the report and the desired format in the parameters. The code is pretty straightforward. The essential thing is that you have to create a temporary file for the result. To create this file your app needs some rights. To make sure the app will have the rights we added an entry in the web.config and the code will read this using the AppSettings property. System management can now assign a scratch dir
Part of web.config
<appSettings>
<add key="TempDir" value ="c:\temp\">add> And this does work.
Blog on, Peter
<appSettings>
<add key="TempDir" value ="c:\temp\">add> And this does work.
Blog on, Peter
Posted
Fri, Jan 16 2004 2:04 PM
by
pvanooijen