CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Once more on Crystal (Access to report file denied)

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


Published Jan 16 2004, 02:04 PM by pvanooijen
Filed under:

Comments

Lance said:

You should check-out Microsoft's new product "Sql Reporting Services" if you do alot of reports and want an alternative to Crystal.

http://www.microsoft.com/sql/reporting/

This is Microsoft's answer to the Crystal Enterprise reporting server. It supports multiple rendering formats (PDF, HTML, Excel, Word, XML, etc.) and offers a web-service based interface for generating or administering the reports.

The best part is that its free if you are using Sql Server 2000. (eg. can setup 1 instance per Sql Server 2k license)

Of course, since this is a server-based solution, it might be a bit overkill for an app that needs only one or two reports total.
# January 17, 2004 9:11 PM

Peter's Gekko said:

Exporting from Crystal Reports to PDF, Word, Excel and HTML
# February 10, 2004 8:36 PM

Madhusudanan said:

Hi
Go to c:\inetpub\wwwroot .There write click and selct properties.Make sure for VS application u provide full control.Else do the same.It will work

Madhu
# March 25, 2004 6:02 AM

Peter van Ooijen said:

Sorry but that sounds like a bad, unsecure idea. Very few sa's will do this for you. My idea is to give the app a safe isoloated scratchpad.
# March 25, 2004 6:21 AM

John said:

if I have a report named "Employee.rpt", what do I pass in for the function protected void exportReport(CrystalDecisions.CrystalReports.Engine.ReportClass selectedReport, CrystalDecisions.Shared.ExportFormatType eft) ? I don't know what selectedReport supposed to be. Please Help!!!
# April 19, 2004 10:16 AM

Peter van Ooijen said:

Selectedreport is a (typed) reportdocument on the webform. You'll find the componenet on the components tab of the toolbox.
# April 19, 2004 10:36 AM

Emmanuel Lambert said:

had the same problem; setting access rights to the \inetpub\wwwroot\"appname" directory solved the problem indeed...
# May 14, 2004 6:47 AM

Peter van Ooijen said:

Changing the rights works without a doubt, but I still find it a bad idea to set the rights in your app dir to delete just because Crystal needs a scratchpad. I beleive it to be a much safer idea to give Crystal a scratch dir where no harm can be done.
# May 14, 2004 9:58 AM

Sekar said:

Hi

It is pool problem, It is not allowing to access multi users at the same time , so Proceed as below

server 2003
---------------
inetmgr > ProjectName(exg prjtest)> properties > virtual Directory > Application pool property as MSSharedPool.

Others
--------
inetmgr > ProjectName(exg prjtest)> properties > virtual Directory > Application Protection > Application protection to high isolated

sekar p
psekar79@yahoo.com
(Kasbah Systems Software)
# May 29, 2004 7:41 AM

Jabbar said:

Hello sekar,
I am a Asp.net programmer I am facing one problem in reporting I want the crystal report to print immediately after i export it to ms-word Can you help me
# June 7, 2004 5:41 AM

dancy said:

In Crystal Reports (CR), is it possible to export separate groups within a report to separate PDF files?

For instance, you have a report grouped on the Country field. You want to export the USA group to one PDF file and the France group to another PDF file
# June 16, 2004 9:31 PM

Peter van Ooijen said:

What about two passes ? Pass one with a selction for the US, pass two for one for France.
# June 17, 2004 1:49 AM

Chris said:

I have a report that needs to be created, but the query is too large for CR to handle since I am getting Server Application Unavailable errors most of the time. I am trying to loop through to create multiple documents using the same query, but changes to the WHERE clause each iteration, but I cannot determine how to export it to pdf and then start the loop again for the next file. Any suggestions? Thanks ahead, much appreciated.
# July 26, 2004 12:39 PM

Kavitha said:

Thanks sekar.
" server 2003 --
inetmgr > ProjectName(exg prjtest)> properties > virtual Directory > Application pool property as MSSharedPool. "

Thanks for your solution. I have changed to Application pool property as MSSharedPool . Now It is working perfectly. Now I am able to export to pdf.

# August 18, 2004 9:59 PM

martin said:

Ping Back??:blog.csdn.net
# October 12, 2004 4:50 PM

jonn said:

MSSharedPool - What are the properties for that application pool?

It doesn't show up in my app pool list.
# October 19, 2004 4:16 PM

Kaya said:

because "MSSharedPool" identity is "Local System" it has read/write access to inetpub/wwwroot directory. i think it's like same solution as Madhusudanan answer

"
Go to c:\inetpub\wwwroot .There write click and selct properties.Make sure for VS application u provide full control.Else do the same.It will work"
# November 19, 2004 9:43 AM

Peter van Ooijen said:

It will work. But is a bad idea. Security matters ! Please do use a dedicated scratch directory ! As my post tries to explain it's no big deal.
# November 19, 2004 11:14 AM

Billy Bob said:

Crystal blows
# February 3, 2005 3:13 PM

Peter van Ooijen said:

I am exploring SQL reporting services. Looks good enough to do the job. And its comes royalty free with sql server. Quite a difference to Crystal server fees.
# February 3, 2005 5:49 PM

Mohan said:

I dont see a MSShared pool. All i see is the default application pool
# June 6, 2005 8:53 AM

Ricardo Puente said:

Peter it works! thanks for you help.

RP
# July 14, 2005 8:04 AM

Ranjith said:

How can we set Summary information to files (excel,pdf,doc) on files generated by Crystal report

# April 27, 2007 1:31 AM

Sadiq, India said:

Thanks for the code. It worked for me :-))

# December 18, 2007 4:29 AM

Hendra said:

Thanks Sekar, this step works with me :

server 2003

---------------

inetmgr > ProjectName(exg prjtest)> properties > virtual Directory > Application pool property as MSSharedPool.

Due to security issue, anyone know, another way out to solve this problem ?

Peter said 'Please do use a dedicated scratch directory ! ' can u please give an example ?

Thank you.

# February 11, 2008 10:27 PM

pvanooijen said:

A dedictaed scratch directory ?

Create a folder somewhere on your web server, and give the asp.net worker process write rights there. What's unclear ?

# February 15, 2008 3:01 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!