Tuesday, June 12, 2012

Crystal Reports SubReport in Asp.Net


In this post Crystal Reports SubReport report, we shall see on how to create a Subreport report using Crystal Reports, and display the results in an Asp.Net page using the CrystalReportViewer control.

A Crystal Report can hold multiple sub-reports, each of which is an individual report by itself. We can create a main report and add sub-reports to each section of the main report, this will help us in designing complex reports.


First create a new .aspx page and drag a CrystalReportViewer control into the page. This control can be located in the Reporting section on the VS.Net 2008 toolbar.

<form id="frmSubReport" runat="server">
<div>   
    <CR:CrystalReportViewer
ID="crvSubReport"
runat="server"
      AutoDataBind="true" />   
</div>
</form>

Now let us create a Typed Dataset with a DataTable to hold the values which will be used to create the Sub-Report. The Typed Dataset will look as follows.



Now add a new CrystalReport file subRptLogInfo.rpt to the Project, this will be sub-report which will be embedded in the main report.

In the Field Explorer (Crystal Reports -> Field Explorer), map the dataset in the Database Expert window as follows.


Open the sub report and design the report as required using the fields added in the Field Expert.


Having designed the sub report, we shall now embed the sub-report in the Main Report. Add a new report file to the project rptMain.rpt, In the Main Report file right click in any of the sections, select the option Insert -> Subreport as follows.




Once this is done, a new popup will open where we need to select the sub report to be embedded in the main report as follows.


Now we are done with the configuration, switch to the code-behind view to query and bind the data to the Report, as follows.

rptMain objRptMain ;
//
// Declare Sub Reports
ReportDocument objSubRpt;

objRptMain = new rptMain();
//
// Add your DataAccess logic here to populate data from the Database, fill the details in the dataset dsReportDetails.

objSubRpt = objRptMain.OpenSubreport("subRptLogInfo.rpt");
objSubRpt.SetDataSource(dsReportDetails.Tables["dtLogInfo"]);

crvSubReport.DisplayGroupTree = false;
crvSubReport.ReportSource = objRptMain;

Save and run the project, you will be able to see the details of the subreport embedded in the main report, here we have added just a single sub-report, we can add multiple sub-reports to a single main reports to the different sections of the main report.


That's it we have created a Sub-Report using Crystal Reports and displayed the same using the CrystalReportViewer control in an asp.net page.

Search Flipkart Products:
Flipkart.com

2 comments:

Anonymous said...

thank you very much. It really helps me ...

Umair Hafeez Mughal said...

Thanks a lot ... it really helped :-)