Home All Groups Group Topic Archive Search About

Can a generic Crystal report be dynamically instantied at runtime?

Author
27 Apr 2006 4:16 PM
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");
}

Author
27 Apr 2006 4:44 PM
Bruce Wood
I don't understand what your TestRpt class is...? Does it inherit from
ReportDocument? Could you post some code?
Are all your drivers up to date? click for free checkup

Author
27 Apr 2006 4:59 PM
Thirsty Traveler
It inherits from the Crystal ReportClass.

Show quoteHide quote
"Bruce Wood" <brucew***@canada.com> wrote in message
news:1146156294.909479.200760@e56g2000cwe.googlegroups.com...
>I don't understand what your TestRpt class is...? Does it inherit from
> ReportDocument? Could you post some code?
>
Author
27 Apr 2006 5:20 PM
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);
Author
28 Apr 2006 3:10 AM
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?

Show quoteHide quote
"Bruce Wood" <brucew***@canada.com> wrote in message
news:1146158422.196885.75270@i40g2000cwc.googlegroups.com...
> 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);
>
Author
28 Apr 2006 5:33 PM
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. :-)
Author
28 Apr 2006 5:38 PM
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.
Author
27 Apr 2006 5:30 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,

"Bruce Wood" <brucew***@canada.com> wrote in message
news:1146156294.909479.200760@e56g2000cwe.googlegroups.com...
>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.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Author
27 Apr 2006 5:32 PM
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.


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Show quoteHide quote
"Thirsty Traveler" <nfr@nospam.com> wrote in message
news:%23BEBXYhaGHA.428@TK2MSFTNGP02.phx.gbl...
> 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");
> }
>
>
Author
27 Apr 2006 6:10 PM
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.
Author
27 Apr 2006 7:11 PM
Thirsty Traveler
I like that idea... much easier to add reports without making coding
changes.

Show quoteHide quote
"tdavisjr" <tdavi***@gmail.com> wrote in message
news:1146161417.544648.230930@y43g2000cwc.googlegroups.com...
>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.
>

Bookmark and Share