Jay
Jay

Reputation: 89

How to load .rpt crystal report file which is a resource in current project

I have created a crystal report and would like to load it to ReportDocument. i.e. the .Load(path) method. The build action for the .rpt is resource. The .rpt file is in the same folder as the .vb file trying to load it.

If I specify the direct path such as "C:/PhotoCrystalReport.rpt" it works but I would like to specify the path where the .rpt will be stored as a resource, it cannot find the file then. I tried the below code but it didn't work (exact same thing worked for a image resource but not for .rpt)

    Dim cryRpt As New ReportDocument
    cryRpt.Load("pack://application:,,,/fstransaction;component/View/Report/PhotoCrystalReport.rpt")

Also tried,

cryRpt.Load("PhotoCrystalReport.rpt")

So, is there any way I can point to the location of this .rpt as a resource?

Upvotes: 1

Views: 4294

Answers (2)

Matthew Peltzer
Matthew Peltzer

Reputation: 138

So, is there any way I can point to the location of this .rpt as a resource?

No.

I have recently come across this same issue, but as far as I can tell there is only one way to load a report -- CrystalDecisions.CrystalReports.Engine.ReportDocument.Load(). This function takes a filename as a string and will not look within assemblies for an embedded resource.

So, you have two options:

  1. Save the report externally (i.e., set the Build Action to "Additional File" and set the Copy to Output Directory to "Copy Always" or "Copy if Newer") and use Load() as it was designed. This is how most online examples suggest you proceed.

  2. Extract the report to a temporary file, and Load() that file. Don't forget to cleanup the temporary file when you're done with the report. There's a bunch of information on how to do this:

Here's how I did it:

var temp = System.IO.Path.GetTempFileName();
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(@"Report.rpt");
using (var fout = System.IO.File.Create(temp))
{
    stream.CopyTo(fout);
}

and then later:

if ((null != temp) && (System.IO.File.Exists(temp)))
{
    System.IO.File.Delete(temp);
}

To get the string that identifies the name of the report, I used the Visual Studio Immediate window in the middle of a debug session. I set a breakpoint just after the assembly variable was created and ran:

? assembly.GetManifestResourceNames()

Upvotes: 0

polkus
polkus

Reputation: 21

I had the same problem and found the solution at this link:

how to deploy WPF application having Crystal Report

string reportPath = System.IO.Path.Combine(Environment.CurrentDirectory, "MyReport.rpt");

properties of your MyReport.rpt file and select Copy Always or Copy if newer.

Upvotes: 1

Related Questions