Can a generic Crystal report be dynamically instantied at runtime?

  • Thread starter Thread starter Thirsty Traveler
  • Start date Start date
T

Thirsty Traveler

The following code will instantiate a specific Crystal report (TestRpt.rpt):

public static void CreatePDF()
{
DataSet ds = TestDAL.GetCrystalDS();
TestRpt rpt = new TestRpt();
rpt.SetDataSource(ds);
rpt.ExportToDisk(ExportFormatType.PortableDocFormat, "TestRpt.pdf");
}

However, I would like to generalize this so the report name can be passed
in... something like this (although I realize this won't work, but you get
the idea):

public static void CreatePDF(string reportName)
{
DataSet ds = TestDAL.GetCrystalDS();
ReportName rpt = new ReportName(); <<< how would I do something like
this at runtime?
rpt.SetDataSource(ds);
rpt.ExportToDisk(ExportFormatType.PortableDocFormat, ReportName +
".pdf");
}
 
I don't understand what your TestRpt class is...? Does it inherit from
ReportDocument? Could you post some code?
 
Ahh. That's in the .cs file generated by Visual Studio when it creates
the rpt, right?

I've never used that class for anything. I consider it junk. I
instantiate my reports at runtime like this:

ReportDocument myReport = new ReportDocument();
myReport.Load("MyReport.rpt");
myReport.SetDataSource(myDataSet);
 
Hi,

Bruce Wood said:
I don't understand what your TestRpt class is...? Does it inherit from
ReportDocument? Could you post some code?

Yes, CR create a class with the same name than the report. The report itself
is created as a embedded resource.
 
Hi,

You can use reflection to create an instance of the report you want to use.
see CreateInstance method for a couple of possible variants.
 
I would choose the option of loading the reports via the filesystem,
like someone suggested, instead of that class VS will create for you.
If you need to make changes to that report, all you have to do is
replace that report with the new one. On the other hand, if you are
loading it via the class name the VS.NET generates for you. You will
have to recompile your project for the changes to take place.
 
Awesome... I implemented this suggestion and it is working and cool.

Now... let's kick it up a notch. I would like to build a stateless web farm
that imports the ReportDocument from a central source, i.e. Sql Server, so
that I don't have to deploy the report to every server or establish a
connection to a central file server.

Can anyone think of a way to do this?
 
I would just put it on a share drive and load it using a UNC path.
That's what we do here. Of course that requires, as you said, a
"connection to a central file server".

But then I can be a bit of a Luddite at times. :-)
 
I just double-checked. ReportDocument.Load() does not take a stream
argument, so you can't load the report from anywhere but a file.

So deploying it on a shared drive seems pretty much your only option.
 
Back
Top