Crystal Reports can be frustrating. Especially the Crystal Reports Engine.
In an ASP.NET 2.0 page I'm trying to load a Crystal Report using the ReportDocument.Load() method, and I kept getting the error:
Access is denied.
System.Runtime.InteropServices.COMException: Access is denied.
The asp.net user already had permission to the .rpt file, but what I didn't know was that Crystal Reports creates its own temp file during the Load() method. This file is put in the directory specified by the environment variable "TMP". You can view the contents of this variable in code by using this method:
System.Environment.GetEnvironmentVariable("TMP")
Giving the ASP.NET user Full Control to this directory will work, but what I really wanted was for Crystal Reports to use a directory that I specified in the web.config file. To do this I set the environment variable, loaded the ReportDocument, and changed the environment variable back.
...
'Get the Crystal Reports Temp directory from web.config
Dim TempRoot As String = ConfigurationManager.AppSettings("CRTempDir")
Dim oldTmp As String = System.Environment.GetEnvironmentVariable("TMP")
System.Environment.SetEnvironmentVariable("TMP", TempRoot)
Dim rpt as New ReportDocument()
rpt.Load("myReport.rpt")
System.Environment.SetEnvironmentVariable("TMP", oldTmp)
...
'Be sure to call this as it will delete your temporary .rpt file
rpt.Dispose()
When you run your web page, Crystal Reports will create a temporary .rpt file using the name of your .rpt and adding a Guid to the end of it. Crystal Reports also writes a .tmp file for some reason. Once Dispose() is called on the ReportDocument both of these temp files will be deleted.
2 comments:
thank you!
this example code i can't read fully. so send that example in my mail yessureshmca@gmail.com
Post a Comment