Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The Baker's Dozen: 13 Productivity Tips for Crystal Reports and .NET : Page 7

These 13 tips show you how to build tools to address common reporting requirements for your business using Crystal Reports with .NET.


advertisement
Tip 10: The Baker's Dozen Spotlight: Print Multiple Charts and a Report Band on One Page
Let's take the concept of printing a chart one step further by building a report with two charts and a detail band on a single page. Figure 10 shows a Product Profile Report with a pie chart depicting sales by region, a line chart showing price trends over time, and a report band showing additional monthly product sales information. This report demonstrates Crystal Reports' ability to display multiple levels of data.

 
Figure 10: A report can show both detail data and multiple charts.
Once again, data drives the definition of the charts. The data definition for a pie chart needs two columns. The first column is the description column to annotate each slice (RegionName); this column maps to the On Change of data option in the Crystal Chart Export. The second column is the data column to be charted (SalesTY); this column maps to the Show Value data option.

The data definition for a line chart is similar to a bar chart. You need two columns to divide and subdivide the data (MonthEnding for the X-Axis and DataType for the line series, to be mapped to the On Change of data option). You need one column for the actual data (RetailPrice for the Show Values data option).

In the previous example, you inserted the bar chart directly into the group header that you defined in the report. Once again, the report contains a group header (this time on the product number instead of the region number). But this time, you'll also need to insert two new sections within the Group Header. To do this, click on the Group Header section on the far right, right-click, and choosing Section Expert. Additionally, you'll need to add two subreports for the Line Chart and Pie Chart, by clicking Insert...SubReport.

Your report shows charts for two different products, so you'll also need to establish a subreport link between the product number in the main report body and the product number in the subreport. Without subreport links, the chart within the subreport cannot identify which group key to chart. Subreport links are established by right-clicking on the subreport and choosing Change Subreport Links. The subreport links screen allows you to define the common key between the main report and a subreport.

At this point you've built the basic report/chart. By following the tip from above to load data for previewing, and right-clicking on sections of each chart to set options, you can customize the appearance of the graphs to suit your needs.

Finally, you can integrate the report into the application by adding it to the project and then calling RunReport with the DataSet and report object name. So long as the DataSet contains all of the datatables that are used in each report and subreport, RunReport will loop through each report and subreport object and set the data sources accordingly.

Tip 11: Printing Dynamic Images
I've seen a few requests on different forums to print a different image for each row or set of rows. The image might be a scanned employee photo, a bitmap of a country flag, or some other type of image.

A technique exists that has been presented both on the Business Object's Developer Zone site at www.businessobjects.com/products/dev_zone, and also in various books on Crystal Reports. In general, you want to create a data column of type Byte, store the image in the Byte column for each row to be represented, generate the XML or XSD file for Crystal Reports, and then drop the image column onto the report in the Crystal Designer.

You can load an image into the Byte column by creating a file stream and binary reader to load the image into a data column:

string cFileName = "MyPicture.JPG"; // must create a stream and a reader FileStream fsStream = new FileStream(cFileName, FileMode.Open); BinaryReader brReader = new BinaryReader(fsStream); DataRow DrNewRow; DrNewRow = MyTable.NewRow(); DrNewRow["myimagecolumn"] = brReader.ReadBytes((int)br.BaseStream.Length); MyTable.Rows.Add(DrNewRow);

You'll probably read the images out of an array, collection, from a folder, etc. Likely, you will use some type of loop. However, you could also use a simple function that receives the name of the image as a parameter.

Tip 12: Printing Rich Text
In a recent application, I had to take rich text that a user entered and preserve any formatting (bold, underline, etc.) when I reported on the text. It turns out this is easy in Crystal Reports: any data element that could contain rich text attributes can be represented as such by right-clicking on the data element in the Crystal Designer, choosing Format Field, and selecting Text Interpretation...RTF Text on the Paragraph tab of the Format editor.

Tip 13: Deploying a Crystal Reports 10 Application
Business Objects provides documentation on deploying Crystal applications. You can find the documentation by searching for the file Deploying_Cr10_Net.PDF on Business Object's support site (http:// support.businessobjects.com). This document covers different Crystal deployment scenarios.

Deploying a Crystal application requires you to create a setup project in Visual Studio .NET. The setup project includes one or more merge modules from Business Objects. You can also find these merge modules on Business Object's support site by searching for the file CR10_NET_MERGE_MODULES.

The PDF file mentioned above covers the steps for creating the setup project. It's worth noting that a setup project cannot be built unless you provide a valid keycode. (Business Objects provides the keycode as part of the registration process when you purchase Crystal Reports).

Closing Thoughts:
You can download the complete source code and project for this article from my Web site at www.commongroundsolutions.net. As this is an ongoing project, I offer a few enhancements from time to time. Check the release notes Word document for details. Sometimes seeing tips like these can spark new ideas for other applications, so maybe you have some new ideas for using other reporting applications.

The objective of this article was to help you become productive with Crystal Reports. You'll find a wealth of technical information on this great reporting tool. As I mentioned above, Business Objects' Developer Zone (http://www.businessobjects.com/products/dev_zone/) contains a tremendous amount of information. These two books, Crystal Reports .NET Programming by Brian Bischof, and Professional Crystal Reports for Visual Studio .NET by David McAmis, are also excellent references.

I can think of a few possibilities to further develop this project that I'll leave in your hands. You may recall that each report had a common header and footer. You could take these and turn them into two subreports (as separate RPT files) that get read in to any report that uses them. You could then use those as template subreports every time you add a new report, as opposed to recreating them (or copy/pasting them) every time.

You might also want to modify the reusable Crystal Manager class to suit to your needs. Sometimes just the process of making one or two changes to an existing library can greatly increase your understanding of how it all works.

Yet another possibility might be modifying these classes to work with ASP.NET. Although the class library is geared toward Windows Forms, you can adapt several of the techniques with some modifications to work within a Web Forms environment.

I encourage you to pull down the code and test project, and to tweak it to your own needs and satisfaction. I plan to maintain and update this library on my Web site. If you have suggestions or ideas you'd like to share, I'd love to hear from you.



Kevin S. Goff is the founder and principal consultant of Common Ground Solutions, a consulting group that provides custom web and desktop software solutions in .NET, Visual FoxPro, SQL Server, and Crystal Reports. Kevin has been building software applications for 17 years. He has received several awards from the U.S. Department of Agriculture for systems automation. He has also received special citations from Fortune 500 companies for solutions that yielded six-figure returns on investment. He has worked in such industries as insurance, accounting, public health, real estate, publishing, advertising, manufacturing, finance, consumer packaged goods, and trade promotion. In addition, Kevin provides many forms of custom training.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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