SubReport In Crystal Reports

In this example i am going to describe how to Create SubReports in Crystal Reports or Crystal Reports SubReport in ASP.NET Using C# And VB.NET. For this i have used two tables from MS SQL database named Employees and Projects.
Main Crystal Report is fetching data from both the tables and is grouped by Project Name

SubReport is used to display information about respective project and fetching data from Projects Table.

Schema for both the tables in shown below in images, create tables accordingly and add relevant data in it to start.


You can click on Images to Enlarge 

 
  
To start , Create a new website in VS and right click on Solution Explorer and select Add new Item > Crystal Report. In the wizard window choose Using the Report Wizard radio button and Standard type in Choose an Expert section.

 
In next screen select Expand OLEDB(ADO) and Choose create new connection 

 
Select Microsoft OLEDB Provider for SQL server and click next 

 
In next screen check the Integrated security checkbox so that report doesn't ask for username and password  Enter you SQL Server name and Select DataBase from the dropdown ,

 
In next window expand and locate your tables you want to use and add them in right pane 

 
  
In next screen select the fields you want to display in main report and add them in right pane , in my case i am showing fields from two tables in main report 
 Now select the field which you want report to be grouped by ( in this example i m grouping report by Project Name)

Select the report style you want and finish the wizard 

 Now to add a subReport Right click in group header section (below Group #1 Name) 
Choose Insert > SubReport , Place the ractangle where you want SubReport to be displayed. a wizard window will open prompltly

 
Enter report Name and click on report wizard buttonin next screen ,
  
  
  
Choose the table and fields you want to use in SubReport in next two screens and click on finish 
Now Insert subReport window will open again , In this window click on Link Tab and select the field on which you want to filter SubReport or the ID of the record to show SubReport. I am using ProjectID in this example.

This is how design of report will look like 

 
And this is how report preview will look

 
Save , build and rum the website. 
Now if you don't want to show SubReport but want to put a hyperlink instead or want to create On-Demand SubReport then do these changes in the design of report 
Right click on SubReport in Design View and select Format Object 

 
In the window opened ,Go to SubReport tab, Change the SubReport name to text you want to show as hyperlink, Check the On-demand SubReport check box and click on ok 
 
 
Now design of report will look like image below 

 

On default.aspx page drag CrystalReportViewer from toolbox and assign CrystalReport we just created as source
Html source will go like this (AutoGenerated)
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
                        AutoDataBind="True" Height="1039px" 
                        ReportSourceID="CrystalReportSource1" 
                        Width="901px" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport.rpt">
</Report>
</CR:CrystalReportSource>

Save, build and run the solution , this is how Crystal Report SubReport will look like


Hope this helps.

If you like this post than join us or share

13 comments:

Anonymous said...

gret


Christo said...

hi,

good post

I want ,how to convert Crystal Report to PDF and

Please Give Example for Cross Tab


Anonymous said...

your tutorial is very good.
this Post is really great help for someone is learn CR


Anonymous said...

Crystal Reports sucks!!!


Anonymous said...

do you have this tutorial in vb .net and mysql database?..


rahul upadhyay said...

gfhf


Anonymous said...

i need use crystal sub report with more than stored procedure or more dataset use.


any one help me ?


nijendra said...

good information about SuB reports in CR


Anonymous said...

your information was great very helpful but i have to export to excel and force a page break on every new sub-report (i have 6) any ideas on how to do this


Anonymous said...

pls send me the C# code to view report dynamically
Email: nazmulmunaz@gmail.com


Anonymous said...

Very very good


Anonymous said...

Super..


Anonymous said...

can u pls send a copy 2 my e-mail?
pinhoong@Hotmail.com
thx in advance


Find More Articles