Can a generic Crystal report be dynamically instantied at runtime?

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");
}
 
B

Bruce Wood

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

Bruce Wood

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);
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 
T

tdavisjr

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.
 
T

Thirsty Traveler

I like that idea... much easier to add reports without making coding
changes.
 
T

Thirsty Traveler

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?
 
B

Bruce Wood

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. :)
 
B

Bruce Wood

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top