Blue Theme Orange Theme Green Theme Red Theme
 
ASP.NET Web Hosting – Click Here
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Crystal Reports » Generating a Report from Multiple Data Sources in Crystal Reports

Generating a Report from Multiple Data Sources in Crystal Reports

If you have to create a report in Crystal Reports from multiple data sources or provide multiple views of the data, you may find sticky situations because CR does not allow you to have multiple Detail sections in a single report.

Author Rank:
Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads :
Total page views :  58959
Rating :
 5/5
This article has been rated :  4 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
ArticleAd
Become a Sponsor



If you have to create a report in Crystal Reports from multiple data sources or provide multiple views of the data, you may find sticky situations because CR does not allow you to have multiple Detail sections in a single report. For example, say you have to display data from two tables on the same report page. Now say, you create two Detail sections and bind each Detail section with a table, you will find second table records repeating within the first table records. 

For example, if I want to generate a report that looks like Figure 1.

Report Header
  - Page Header
       o Detail - Data from Customers table
       o Detail - Data from Orders table
  - Page Footer
Report Footer

Figure 1.

To do this, you will have to create two details sections on the same level, not recursive.

The work around for this is using sub reports. What we do is to create two sub reports, one for Customers and one for Orders. These sub reports have nothing but a single Detail section.

Now we create two Page Header sections on the main report and put a sub report on each Page Header sections. The new report looks like Figure 2.

Report Header
  - Page Header 1
      o Sub Report 1
  - Page Header 2
      o Sub Report 2
      o Detail Section (Hide it)
  - Page Footer
Report Footer

Figure 2.

In the above format in Figure 2, we treat each sub report as a separate report. Now you can bind each sub report with a separate DataSet or DataTable. 

For example, if we have data coming in a DataSet from two tables-Customers and Orders, the following code binds each DataTable to a separate sub report: 

Dim report As New DynamicReport
report.SetDataSource(ds)
report.Subreports.Item("SubReport1").SetDataSource(ds.Tables("Customers"))
report.Subreports.Item("SubReport2").SetDataSource(ds.Tables("Orders"))
CrystalReportViewer1.ReportSource = report
CrystalReportViewer1.DataBind()

Similarly, your data can be coming from multiple data sources; you can bind as many as sub reports with different data sources.

Note: In the above code, DynamicReport is the class name of crystal report. For example, if you have a crystal report called "MyReport.rpt" and you add this report to the VS.NET project, you will see a class "MyReport.cs" or "MyReport.vb" depending on the language you choose. Make sure you change DynamicReport with your class name "MyReport".

Summary

In this article, we learned how to create multiple sub reports and bind them with multiple data source using DataSet. Using the same approach, you can get data from multiple data sources in a single report.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software consultant, architect, author, MCP, MVP, and founder of C# Corner. He has over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries including Microsoft, Unisys, Barclay’s, Centocor (J&J), McGraw-Hill, Excelon, PMI, and University of Pennsylvania Hospital. Since year 2000, he is been working with, ASP.NET, SQL Server, C# and .NET. His latest experience and interest is Silverlight, WPF, WCF, XAML and .NET 3.5. If you need any consulting in ASP.NET, AJAX, WPF, WCF, or XAML, contact him at mahesh AT c-sharpcorner DOT com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
Can't find report 'DynamicReport'Sarim10/21/2006

Can you please tell me which name space or DLL i have to add in my project to find 'DynamicReport' class. i m using CR 10, and current i m using these three namespaces in my project.  

using CrystalDecisions.Shared;
using CrystalDecisions.Web;
using CrystalDecisions.CrystalReports.Engine;

but when i try to use your code i can't find 'DynamicReport' what you use in your Code.

 

 

 

Reply | Email | Delete | Modify | 
Re: Can't find report 'DynamicReport'Mahesh10/21/2006
DynamicReport is a report you created using VS.NET or Crystal Report. If you create a .cr file, you would see a class for that .cr file. For example, if you add a Crystal Report file using VS .NET called ABC, a class ABC.vb or ABC.cs (depending on the language) will be added to your project. So you will replace DynamicReport with ABC.
Reply | Email | Delete | Modify | 
vikramvikram11/28/2006

i am using VS 2003 and i could not find the property called

Subreports.Item

Reply | Email | Delete | Modify | 
 
 
Re: vikramMahesh11/29/2006

I am using VS 2003 also but also have CR 10 installed.

VS 2003 uses CR 9.0 by default and it is possible Report class does not have that property but there should be something similar.

Reply | Email | Delete | Modify | 
Can i ask for other examples..Edmond1/26/2007
hi.. this is edmond. can i ask other examples...
Reply | Email | Delete | Modify | 
 
 
Re: Can i ask for other examples..Mahesh1/29/2007

Crystal Reports section has more example. Click on Crystal Reports in the left side menu or go from Technologies menu.

Do you need any specific sample?

Reply | Email | Delete | Modify | 
 
Re: Re: Can i ask for other examples..Srinivasa Rao2/19/2007

Hi Mahesh,

follow line doesn't work in VS2005(C#.Net).

report.Subreports.Item("SubReport1").SetDataSource(ds.Tables("Customers"))

Could You tell me what is the equal command in VS2005

thanks and regards

Srini

Reply | Email | Delete | Modify | 
 
Re: Re: Re: Can i ask for other examples..Mahesh2/20/2007

Srini,
Make sure your report has a sub report called "SubReport1" and your DataSet should also have a DataTable named "Customers". If you are getting data from the database and filling it direct, use "ds.Tables(0)" instead, which will return first table no matter what the table name is.

Reply | Email | Delete | Modify | 
 
Re: Re: Re: Re: Can i ask for other examples..Srinivasa Rao2/20/2007

I face following problem

(1)report.Subreports after that I am not getting "Item" keyword

Yes Report had SubReports name "SubReport1"

Reply | Email | Delete | Modify | 
 
Re: Re: Re: Re: Can i ask for other examples..Srinivasa Rao2/20/2007
I forgot to tell to you. I am using CrystalReports XI with C#.Net 2.0
Reply | Email | Delete | Modify | 
 
Re: Re: Re: Can i ask for other examples..marshall5/23/2007

marshall_martin@bcbstx.com

I use this and I can set the sub report datasource but the problem it says that it cannot find my parameters in my sub reports.

 crLtr_New.Subreports.Item("ltrIneligiblewithSum.rpt").SetDataSource(Me.DsAll1.Tables("dbo_usp_WS_RACI_Summary_Reports"))
        'crLtr_New.SetParameterValue("prmRACI", Me.DsAll1.Tables("dbo_get_ineligible_with_sum").Rows(0)("group_raci"))
        'crLtr_New.SetParameterValue("prmAddr_Ln1", Me.DsAll1.Tables("dbo_get_ineligible_with_sum").Rows(0)("addr_ln_1"))
        'crLtr_New.SetParameterValue("prmAddr_Ln2", Me.DsAll1.Tables("dbo_get_ineligible_with_sum").Rows(0)("addr_ln_2"))

Hope it helps,
MECCA

Reply | Email | Delete | Modify | 
 
Can i ask for Amal11/20/2007
I want to ask how can i write a query in Crystal Reports in C# .net 2003,not make the report by wizard thanks
Reply | Email | Delete | Modify | 
Using Grouping on actual report with sub reportsJames2/20/2007
we are pulling data from 2 sub reports in page header, however we'd like to group on the actual report at the same time. Can this be done? If so how?
Reply | Email | Delete | Modify | 
need your helpMony4/23/2007
Hi, Pls can you show me how to pass few parameters from form1 to form2(Report) in VB 2005? I am a new developer. Thanks ssmony@gmail.com
Reply | Email | Delete | Modify | 
 
 
Re: need your helpMahesh4/24/2007
If you click on Crystal Reports section of this site in the left side, you will see articles on "How to pass parameters in a Report".
Reply | Email | Delete | Modify | 
Help To Create ReportsKartheek reddy6/20/2007
Hai Mahesh, I am Kartheek, http://www.codeproject.com/useritems/Le_Quang_Anh.asp using the above link I have created Reports successfully. But Now I have to generate a Report Using two datasets as created in the above example in the site. I tried that using ur example but I amnot able to do that... so can u plz help to Generate a single report using two or more datasets as shown in the above example.
Reply | Email | Delete | Modify |