Creating Sub-Reports Using Crystal Reports
 
Published: 11 Jul 2008
Abstract
This article demonstrates the creation of sub-reports using Crystal Reports. After providing a short overview of the database design, the author delves deeper into the report building process in a series of steps with associated screenshots. Towards the end of the article, he shows us how to display the report on a web page.
by Mohammad Azam
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28681/ 544

Introduction

Reports allow the user to view the complex data in a readable format. The data can be stored in a variety of places and can be pulled out by the reporting tool to generate a user friendly report. In this article we will demonstrate how to create sub reports using Crystal Reports.

Database Design

Our database "EasyShopping" consists of two tables: "Categories" and "Products." The Categories table contains all the different categories available to the user. And the Products table contains all the products related to different categories. Check out the database diagram for both the tables in the database.

Figure 1

Understanding the Hierarchical Data Display

Let us first understand what we meant by hierarchical data display. As you can see from the database diagram, the "CategoryID" is a foreign key in the "Products" table. This means that a single category can have multiple products. In other words, it is a One-to-Many relationship. Our task is to display a category name along with its products. This process will be repeated for every category in the database. Here is the diagram that explains the flow of the report.

Figure 2

Now it is time to start designing the report.

Designing the Report

The first task is to add a Crystal Report to your project. Right click on your project and select "Add New Item." Click on the Reporting category and select "Crystal Report." Check out the image below to get a better idea.

Figure 3

We are naming our report as "CategoryReport." Click the "Add" button and you will see the following screen.

Figure 4

Select the "Using the Report Wizard Option" and click "OK." The next screen will ask to configure the data source for the reports. Since our data is contained in the SQL SERVER database, we will create a new connection as shown below.

Figure 5

Double click on the "Make New Connection" and the following screen will pop up.

Figure 6

Select "Microsoft OLE DB Provider for SQL SERVER" as shown above and click the "Next" button. The next screen will allow you to connect to a database. Check out the screen shot below.

Figure 7

 

The Integrated Security is checked because the SQL SERVER is hosted on the same machine from where the application is running. Click Next to continue to the advanced OLE DB information screen as shown below.

Figure 8

Click finish and you will see figure 3 showing the connection established. Drill down to the "EasyShopping" database and add the "Categories Products" table to the selected tables by clicking the ">" button.

Figure 9

Click "Next>" and you will be taken to the "Link" screen which allows you to link different tables together.

Figure 10

We are not going to link any tables in this example so you can simply press the "Next>" button to move forward. The next screen is the "Field Selection Screen" which allows you to select the fields you want to display in a report. Select the "CategoryName" field from the options on the left and press the ">" button to add it to the selected fields to the right.

Figure 11

Press "Next>" to move to the Grouping Screen. We will not include any group for this demo.

Figure 12

Press "Next>" to move to the "Record Selection" screen. We will not be selecting any records so you can bypass that screen.

Figure 13

Click "Next>" to move to the "Report Style" screen. You can select any style you want, but for this demo we will be using the "Red/Blue Border" style.

Figure 14

Click the "Finish" button to finally end the wizard steps. Click on the "Main Report Preview" button at the bottom of the screen to see the preview of the generated report.

Figure 15

As you can see from the screenshot, the report is displaying the "CategoryName." We are half way done. Now we need to add a sub-report that will display all the products in each category.

Adding Sub-Report

The next task is to add a sub-report to the current report. The sub-report will enable the display for the products of each category. To add a sub-report right click on the current report's "Details" section and select "Insert -> Section" as shown in the screenshot below.

Figure 16

In the "Insert Section" window select "Details" and click the "Insert" button at the top of the screen.

Figure 17

This will add a separate "Details" section as shown below.

Figure 18

Click on the "OK" button to finish adding the section. When you are finished you will see a new section added to the report.

Figure 19

 

 

Now, you need to add a sub-report inside the "DetailSection2(Details b)." Right click inside the Details b section and select "Insert -> Sub-Report" as shown in the screenshot below.

Figure 20

When you select the sub-report option it will create a rectangle where the sub report will be displayed. Simply place the sub-report rectangle inside the "Details b" section. This will open an insert Sub-Report window.

Figure 21

Then click on the "Report Wizard" button and select the "Products" table from the right panel. Move the "Products" table to the left panel by using the ">" button.

Figure 22

Click "Next>" to continue. The next screen will allow you to select the "Fields" that you want to display on the report. We are going to select "ProductName" as shown in the screenshot below.

Figure 23

Click "Finish" to end the wizard. The "Finish" button will lead to the "Insert SubReport" window. Select the "Link" tab to display the link screen. The link screen will allow you to set the parameters that will be used to fetch the sub report.

Figure 24

The sub-report accepts a parameter named "CategoryID" from the master report. This will allow us to create the parent-child relationship between the data. When you preview the above report you will see the following output.

Figure 25

The above screenshot shows the hierarchical relationship between the "Category" and its "Products."

Displaying Report on a Webpage

The last step is to display the report on the webpage. This is accomplished by using the CrystalReportViewer and CrystalReportSource control. The implementation is shown below.

Listing 1

<CR:CrystalReportViewer ID="CrystalReportViewer1"  runat="server" 
AutoDataBind="true" ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1"  runat="server">
<Report FileName="CategoryReport.rpt">
</Report>
</CR:CrystalReportSource>

The CrystalReportSource control fetches the report by using the Report FileName attribute. The report is displayed on the webpage by the CrystalReportView control. Take a look at the final output that is displayed on the webpage.

Figure 26

Conclusion

In this article we learned how to display hierarchical data using Crystal Reports. As you saw, most of the operations associated with building a report involved less coding and more drag, drop things. This benefits the developer by allowing him or her to concentrate on the outer design rather then the inner workings.

Downloads

[Download Source]


Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 3 and 5 and type the answer here:

User Comments

Title: Impressed   
Name: Salil
Date: 10/27/2009 12:55:04 PM
Comment:
you rock..thanks a lot..
Title: Thanks   
Name: Krishna
Date: 10/5/2009 4:00:37 AM
Comment:
Amazing....Thanks a lot for your help in understanding sub reporting in crystal reports.

Can you let me know how we are supposed to do the same withput using "ReportSource" and by using code in the Script..

My EMail ID is kkumar181286@gmail.com

Thanks a million...
Title: Subreport Printing   
Name: Pankaj Rai
Date: 9/24/2009 2:35:18 AM
Comment:
Hi,
I am creating subreport using dataset .It's working fine but.When I click on print page will went to main report.
What I have to do .Please reply ASAP.


Thanks in Advance
Title: SUBREPORTS   
Name: Steve
Date: 9/1/2009 9:53:30 AM
Comment:
Hi I am creating a wrapper to house 9 sub-reports (because they have to print in order and the sql code is copywritted), problem is that i will be passing parameters to the rpt,from a user-interface and I am having issues figuring out how to code it in crystal. Could you please help??
Title: As usual Great article for guyz like me   
Name: Jay
Date: 6/7/2009 2:29:35 PM
Comment:
One nice thing about Azam is, just follow his doc you will become good developer. i like his comments and screen shots. Thans a lot Azam.
Title: subreport   
Name: Thanasekaran
Date: 5/4/2009 5:45:24 AM
Comment:
Hi
I am creating a sub report and it's working fine
the problem is in run time the curser move to sub report it's show the hand Symbol and if it's click that time main report are hiden
how to control this problem
pls help
Title: Dr   
Name: Desouki
Date: 5/2/2009 6:14:55 AM
Comment:
very useful article, thanks
Title: Crystal SubReport   
Name: madhu
Date: 4/20/2009 5:01:59 AM
Comment:
Sir

I am using crystal 11 and i hv created a main report and want to insert 2-3 sub reports but data is not comming in sub reports, pl help me.
Title: Crystal SubReport   
Name: Ravi
Date: 3/20/2009 9:02:29 PM
Comment:
Sir

I am using crystal 11 and i hv created a main report and want to insert 2-3 sub reports but data is not comming in sub reports, pl help me.
Title: Sub Reportimg   
Name: Prashanth A
Date: 2/3/2009 1:13:55 AM
Comment:
This article is great!!!!!!!!! and gives all the information in a very understandable format thanks for such a great article .
keep up the good work
Title: Subreporting   
Name: Gunjan Singh
Date: 12/31/2008 5:21:35 AM
Comment:
This aricle is very useful sir.
I got good concept from this
Thanks
Title: subreport   
Name: jaipalreddy
Date: 11/18/2008 5:50:12 AM
Comment:
this artical is super......and gives all the information in a very understandable format thanks for such a great article .keep up the good work.
Title: Sub reports article on CR   
Name: prashanti t
Date: 10/21/2008 1:30:26 PM
Comment:
this article is great!!!!!!!!! and gives all the information in a very understandable format thanks for such a great article .keep up the good work.
Title: Sub reports article on CR   
Name: Colin Gray
Date: 8/12/2008 5:50:43 PM
Comment:
Hi Mohammad,

Thanks for the article on sub reports. They're often great to use and can get report developers out of sticky positions. I would be careful about showing subreports for situations that can be address with groupings though... Sub Reports, especially dropped into detailed sections, can have a significant impact on performance. Many developers use them to mash data together where links in the database don't exist or to position the data more with more flexibility. Just thought I would comment.

cheers

Colin Gray
Developer Marketing Manager
Business Objects, an SAP company.
Title: nice information   
Name: anver
Date: 7/25/2008 6:20:26 AM
Comment:
thnx azam to share such avaluable stuff of ctystal report with us.
continue ur sharing and expand knowledge for us in future.
have a nice future

Product Spotlight
Product Spotlight 



Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2009 ASPAlliance.com  |  Page Processed at 11/21/2009 3:52:22 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search