RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: 13 Productivity Tips for Crystal Reports Redux : Page 5

This article is a redux, or "Version 2.0" of the Baker's Dozen article on Crystal Reports that ran in the January/February 2005 issue of CoDe Magazine. The article updates some of the classes, covers some new features in Crystal Reports XI, and demonstrates techniques to help developers get the most out of Crystal Reports.

Tip 7: Details on Export Options
A few months ago, a reader named Rob Richardson sent me an e-mail regarding my original Crystal Reports article. He pointed out that the original export library in my code used the PdfRtfWordFormatOptions class to set the format options for the export, regardless of the export destination. He then suggested that I use the ExcelFormatOptions class to set format options for exports to Microsoft Excel (see Figure 17).

So, the export function in Listing 1 reflects this change. Thanks very much, Rob!

Figure 17: Using IntelliSense in VS 2005 to view available ExcelFormatOptions for Crystal Reports.
Tip 8: Drill Down to Controls in the Object
I want to programmatically format a specific report object, where the object does not contain a format display formula at design time. While I can set most formatting options, such as font characteristic, through display formulas, there are other options I cannot set. Therefore, I want to handle this programmatically at run time.

A few report objects, such as the line object and box objects, do not contain formula fields to conditionally suppress the objects. In some instances, I might be able to place the line/box objects in a section and use a formula for the section; in other cases, the location of the line/box objects may not lend itself to a section.

Some developers view reports as busy work. But they are the bridge between the data that you see, and the information that clients and users expect from the system.
Fortunately, the Crystal Reports object model allows me to drill down through the report and section area objects and access properties that aren't accessible through formulas. The following code shows two different ways to suppress a line/box object. If I know the actual names of the report objects, I can access them specifically.

   // uses the namespace
   // CrystalDecisions.CrystalReports.Engine 
   RPT_Invoice oReport = new RPT_Invoice();
   Section oSect = 
        ObjectFormat.EnableSuppress = true;
        ObjectFormat.EnableSuppress = true;
If I don't know the specific object name, but I do know that the line and box that I want to suppress are the only line and box objects in the section, I can loop through the objects in the section area, check each object's type, and suppress it accordingly.

   RPT_Invoice oReport = new RPT_Invoice();
   foreach (ReportObject orb in oSect.ReportObjects)
   if (orb is LineObject || orb is BoxObject)
             orb.ObjectFormat.EnableSuppress = true;
If you're already familiar with this technique, you may ask if you can use this to tap into other report objects, specifically options within a Crystal Reports chart. Unfortunately, chart objects are more of a "black box" —you cannot drill into the object hierarchy to set things like pie chart colors and other aspects of a chart.

Some developers choose to get around this by using a third-party charting tool to build the graph to an image, and then import the image into the report. Some use the product CRChart by ReCrystallize Software, a tool that expands the charting capabilities of Crystal Reports.

Tip 9: Using the Pull Model for Internal Applications—Stored Procedures, and Authentication
The previous article and this article have stressed the push model, where you use XML/dataset data to push into the report at run time. This is the most effective model, and the ONLY model when working in a distributed environment. However, when building internal applications, I may just want the reports to pull results directly from a database stored procedure using the required login credentials.

I have strived to design my reports against a common format (XML/XML Schemas) and to keep database information out of the report. However, some applications may run directly off an internal database server. These systems will often have the reports, database server, and application all under one roof. In this situation, developers will commonly design the reports against an internal server using database stored procedures as the report's data source. Then they will set up a basic interface where internal users will run them off a different server that contains the same stored procedures.

Crystal Reports, like any other report writer, is a presentation vehicle for your data. So to repeat what was I two years ago, building good reports is all about three things: data, data, and data.
To accomplish this, I need to do the following, as shown in Listing 3:

  1. 1 Create an instance of the report.
  2. 2 Create instances of the Crystal Reports TableLogOnInfo and ConnectionInfo objects.
  3. Set the authentication properties in the ConnectionInfo object.
  4. Apply the authentication values to each table in the result sets.
Keep in mind that the authentication code in Listing 3 must be applied to every data table in the report, which includes any subreports. Sometimes you will see a "login failed" message from Crystal Reports when generating reports—this is often due to one or more of the report data tables not being covered by authentication code.

Tip 10: Page and Section Control
I need to print addresses on a report, where some address records need two lines and others only need one. If an address line is blank, I want the report to suppress the address line and move the following line up. That way, they'll print in proper form, so that no empty address lines appear in between.

Additionally, the report also contains a standard terms and conditions clause that prints in between sections of a report. The content is variable and can span many lines. I want to make sure that the content always prints on the same page, even if it means starting a new page.

Figure 18: Using the Crystal Reports Section Expert to suppress blank sections.
Figure 18 shows how I can address the issue of multiple addresses where some can be blank. I can right-click in the main report body and load the Section Expert. This allows me to insert multiple subsections within a section, one for each possible address line. For each inserted section in the Section Expert, select the check box to Suppress Blank Section.

To protect a section against being split across two pages, select the check box in the Section Expert to Keep Together (see Figure 18). In this example, if I place a Terms and Conditions column in a separate section, and turn on the option to Keep Together, the content will always print on the same page. This is basic "widow/orphan" protection for a section.

If you need more expansive window/orphan protection for an entire group, check out the same option in the Crystal Reports Group Expert.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date