Chapter 14 - Crystal Report Object Models


The reason that you have so much power to modify reports is because .NET treats every report as an object-oriented class. The entire object model is exposed to your .NET program. Whether you program with VB.NET or C# isn't important. The object model can be accessed by any of the .NET languages.


Get the best selling Crystal Reports .NET book on the market! Learn to build .NET 2003 reporting solutions with this expert guide.
Buy at Amazon.com
Buy at Amazon.co.uk

This is an excerpt from the book Crystal Reports .Net Programming. Click to read more chapter excerpts.

Crystal Reports C# Code Listings

The C# code listings are equivalent to the VB.NET code listings found within Chapter 14.

Listing 14-1. A template for modifying reports.

private void Form1_Load(object sender, System.EventArgs e)
{
    CrystalReport1 MyReport = new CrystalReport1();
    //Call all report modification code here.
    //For illustration purposes, I'm calling a generice method that changes the report title.
    //The code for ModifyTitle() isn't shown here.
    ModifyTitle(MyReport, "Runtime Demo");
    crystalReportViewer1.ReportSource = MyReport;
}

Listing 14-2. Template for ASP.NET pages.

private void Page_Load(object sender, System.EventArgs e)
{
    CrystalDecisions.CrystalReports.Engine.ReportDocument MyReport;
    if (!IsPostBack) {
        MyReport = new CrystalReport1();
        //Call all report modification code here.
        //For illustration purposes, I'm calling a generice method that changes the report title.
        //The code for ModifyTitle() isn't shown here.
        ModifyTitle(MyReport, "Runtime Demo");
        Session["MyReport"] = MyReport;
    } else {
        MyReport = (CrystalDecisions.CrystalReports.Engine.ReportDocument)Session["MyReport"];
    }
    CrystalReportViewer1.ReportSource = MyReport;
}

Listing 14-3. Forcing garbage collection in an ASP.NET page.

private void WebForm1_Init(object sender, System.EventArgs e)
{
    if (MyReport!=null) {
        MyReport.Close();
        MyReport.Dispose();
        GC.Collect();
    }
}

Listing 14-4. Change a report’s printer settings.

CrystalReport1 MyReport = new CrystalReport1();
MyReport.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
MyReport.PrintOptions.PrinterName = "HP LasterJet510";
MyReport.PrintToPrinter(1, false, 0, 0);

To read all my books online, click here for the Crystal Reports ebooks.