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

This entry was posted in Out+of+control. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://twitter.com/KarthikRamas KarthikRamas

    Saved my day! Thanks

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    This is a commercial. I’m not going to delete it but IMHO tools like that are no good at all. Perhaps they have a far better UI than Crystal RS or whatever.

    But the majority of the end users don’t understand the structure of a database. An object model comes closer to the users view but it takes an O-R mapper to map that to a database and a domain expert to translate it to the end user.
    And a well designed report to give the end user the (correct) information he needs.

    Tools like this set the expectations of management higher than the reality of the end user.

    To say it rude: There is no rocker science for the mentally disabled.

  • http://www.stonefieldquery.com Kooper

    Eliminate the IT reporting bottleneck – Stonefield Query is designed to put Business Intelligence reporting into hands of the end user where it belongs. Stonefield Query Self-service BI reporting solution allows your executive, manager/staff, and business anylists to take advantage of the wealth of information stored in your database system when they need it. Casual users can create elegant and persuasive reports in minutes with little or no technical knowledge.

    Visit our website to download a 30 day trial.
    http://www.stonefieldquery.com

  • B.

    change permissions on c:/windows/temp at the server
    give modify right to user who is set on the application pool , ex NETWORK SERVICE (default for anonymous )

  • http://codebetter.com/blogs/peter.van.ooijen pvanooijen

    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 ?

  • Hendra

    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.

  • Sadiq, India

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

  • Ranjith

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

  • http://none Ricardo Puente

    Peter it works! thanks for you help.

    RP

  • Mohan

    I dont see a MSShared pool. All i see is the default application pool

  • http://codebetter.com/blogs/peter.van.ooijen/ Peter van Ooijen

    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.

  • Billy Bob

    Crystal blows

  • Peter van Ooijen

    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.

  • Kaya

    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"

  • jonn

    MSSharedPool – What are the properties for that application pool?

    It doesn’t show up in my app pool list.

  • Kavitha

    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.

  • Chris

    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.

  • Peter van Ooijen

    What about two passes ? Pass one with a selction for the US, pass two for one for France.

  • dancy

    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

  • Jabbar

    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

  • Sekar

    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)

  • Peter van Ooijen

    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.

  • Emmanuel Lambert

    had the same problem; setting access rights to the \inetpub\wwwroot\"appname" directory solved the problem indeed…

  • Peter van Ooijen

    Selectedreport is a (typed) reportdocument on the webform. You’ll find the componenet on the components tab of the toolbox.

  • John

    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!!!

  • Peter van Ooijen

    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.

  • Madhusudanan

    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

  • Lance

    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.