Generating a Crystal Report from a Data Source

Generating a Crystal Report from a Data Source

Another goody that's packed away in Visual Studio .NET is the Crystal Report viewer. Crystal Reports for Visual Studio .NET is the standard reporting tool for Visual Studio .NET. You can host reports on Web and Windows platforms and publish these reports as Web services on a server.

Building a Crystal Report

Let's step through how to generate a report from any data source. (This example assumes that you have Microsoft Access 2002 installed on your computer.) Start a new Windows application project, and give it the name CrystalExample. The default form will be added to the project. Two steps are required to display a Crystal Report in your Visual Basic .NET program. First you must build a report, and then you add a Crystal Viewer object to show the report. Let's start by adding a report control.

  1. From the main IDE menu, select Project | Add New Item and select Crystal Report, as shown in Figure A-11. Click Open.

    Figure A-11

    Add a report control to the project.

  2. The Crystal Report Wizard will then prompt us for the type of report we want to build, as shown in Figure A-12. Keep the default standard report, and then click OK.

    Figure A-12

    Keep the default standard report.

  3. The Crystal Report Wizard now displays the Standard Report Expert dialog box, as shown in Figure A-13. For this project, click the plus sign next to the OLE DB (ADO) folder in the Available Data Sources list box.

    Figure A-13

    The Standard Report Expert dialog box.

  4. You'll next see the OLE DB (ADO) dialog box, as shown in Figure A-14. This time we will select an Access database to show the diversity of these tools. Select Microsoft Jet 4.0 OLE DB Provider, and then click Next.

    Figure A-14

    The OLE DB (ADO) dialog box.

  5. We next need to tell the wizard which database we want to connect to. If you have Access installed on your machine, click the button after the Database Name text box (shown in Figure A-15) and select the Access Northwind.MDB database. Click Next.

    Figure A-15

    Select the Northwind database.

  6. The Crystal Report Wizard now displays the Advanced Information dialog box. We don't need to change any information for this example, so click Finish. In the Standard Report Expert window, select the Categories and Products tables for our report, as shown in Figure A-16. Click Next.

    Figure A-16

    Select the Categories and Products tables.

  7. On the Links tab in the Standard Report Expert dialog box, keep the defaults, as shown in Figure A-17. You can see that the wizard mapped the primary key in Categories to the foreign key in Products. Click Next to display the Fields tab.

    Figure A-17

    The primary key in Categories is mapped to the foreign key in Products.

  8. From the Categories table, select CategoryName and Description. From the Products table, select the ProductName, QuantityPerUnit, UnitPrice, and UnitsInStock fields, as shown in Figure A-18. Click Next to display the Group tab.

    Figure A-18

    Select the fields shown here.

  9. We want to group the report by Categories and Products within each Category. Go ahead and select CategoryName and ProductName to group on, as shown in Figure A-19.

    Figure A-19

    Select CategoryName and ProductName to group on.

  10. Click Next several times to accept the default settings for the Total, Top N, Chart, and Select tabs. When you get to the final tab, titled Style, add the title Category - Product Report, as shown in Figure A-20.

    Figure A-20

    Add the title Category - Product Report.

  11. Click Finish. That's all there is to building a report. The new CrystalReport1.rpt, shown in Figure A-21, will be added to your project. Be sure to right-click the CrystalReport1 tab and click Save CrystalReport1.

    Figure A-21

    The report is designed.

Getting Ready to View Our Report

We now have our report built, so it's time to set up the mechanism to view it. Luckily this is pretty easy. Just like we built the report without writing any code, we can do the exact same thing when we set up the viewer. Drag the CrystalReportViewer control from the Windows toolbox to the default form.

Right-click the CrystalReportViewer, and select Properties. Select the Dock property and select Fill, as shown in Figure A-22. This option will cause the viewer to fill the entire client area of the form.

Figure A-22

Select the Dock property and select Fill.

The completed report is shown in Figure A-23. As you can see, the CrystalReportViewer control is pretty sophisticated. You can use the built-in functionality to page and print the report. When the application is running you can click the Export Report button (the envelope with the red arrow) to export the report to an Adobe Acrobat .pdf, a Microsoft Excel .xls, a Microsoft Word .doc, or a Rich Text Document .rtf file. You can also magnify areas or even find specific text within the report. All of this power is built into the viewer.

Figure A-23

The completed report.

All that's left is for us to tell the viewer which report we want displayed. Right-click the viewer once more, and select Properties. Select the ReportSource property, and click Browse. Find the CrystalReport1.rpt file we just built and select it. Now run the program. The program should look like Figure A-24.

Figure A-24

The Crystal Report program in action.

Pretty powerful for not even having to write a single line of code. Explore the Crystal Reports help files to see how you can easily modify the default report to look like just about anything you want. As you might guess, a report added to a project is a strongly typed report. A report source file, containing a report class specific to this report, is automatically created for it. This report class has ReportClass as its base class.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net