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.

avdhesh singhPosted Jun 27, 2015, 9:08 AM
can you explain this in C#
arishPosted Sep 2, 2011, 8:49 AM
i want to disply the record from two different storeprocedure into one crystal report what can i do for that please tell me
Vishal SharmaPosted Oct 21, 2010, 11:06 AM
Is it possible to use some sort of a sub-report template which can be attached to the main report dynamically in a loop via code behind? I have 4 very different data tables each for a certain key value and the number of key values can be several but these 4 data tables for a key value have to appear together one after the other as if 4 static sub reports were laid out, one each for each data table. the tricky part is the same thing has to be repeated for subsequent key values. please let me know if you are able to read through my description or i need to provide an example. thanks.
preeti donekalPosted Aug 6, 2010, 3:57 AM
Hi, I have areport for which the source is excel.When I publish the crystal report in infoview,it prompts for db login while refreshin the report.Even though I set theoption to use the same database login at CMC level,it did not work I have created a DAO connection forexcel and then generated the report. Kindly suggest a solution as it is not a problem only with excel source.
Harmeet SinghPosted Jul 15, 2010, 7:12 AM
Hi This Is a Great site and i have some serious trouble. i am trying to bind my report through stored procedure as it is not getting connected to the SQL server (its not showing the server and database name ) so i created a stored proc binding many tables its working fine ..i am able to preview data in dataset but not in the report i have added the fields and the parameter but its not working . plz help me out as i m new to this.
minakshieditedPosted May 11, 2010, 7:48 AMEdited May 11, 2010, 7:57 AM
Hi Mahesh , In my application I have two tables bill & bill details which data i want to display using crystal report. Tables are linked with the foreign key relation. How can I do this? Thanks, Minakshi.
zohaib sheikhPosted May 9, 2010, 6:57 AM
dfgfgf
CatincaPosted Mar 18, 2010, 4:33 PM
Hi, Can somebody explain how to generat a report using 2 different database? I will realy appreciate. Cathy.
joshuaPosted Oct 8, 2009, 4:56 PM
Thanks for the informative article. I set up my report like you specified and it prints fine if the report doesn't exceed 1 page. I hid the Details section and set Section 2 and PageHeaderSection2 to NewPageBefore = true, but that didn't help. Do you have any ideas? Thanks.
asm asmPosted Sep 7, 2009, 4:06 AM
please help me as i am not getting data in crystal report using stored procedure my code is as Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("satinconnectionstring").ConnectionString) con.Open() Dim cmd As New SqlCommand cmd.Connection = con Dim dat As Date = Today.Date Dim M As Int16 = Today.Day If M >= 28 Then M = 28 End If Dim W As String = Today.DayOfWeek.ToString cmd.CommandText = "EXEC MYSP_DEMAND_SHEET '20983','" & M & "','" & W & "','" & dat & "'" Dim da As New SqlDataAdapter(cmd.CommandText, con) Dim ds As New DataSet da.Fill(ds) Dim myReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument myReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument() myReportDocument.Load(Server.MapPath("CrystalReport2.rpt")) myReportDocument.Database.Tables(0).SetDataSource(ds) CrystalReportViewer1.ReportSource = myReportDocument CrystalReportViewer1.DataBind() please help me in this regard
Liz LuceroPosted Aug 12, 2009, 3:17 PM
Thank you for your article. It is so hard to find comprehensive information about Crystal Reports.
Kapil AgarwalPosted Jun 1, 2009, 7:54 AM
Hi Mahesh, I have same requirement accept that I do the same thing in Asp.net 2.0 with the XSD file. I fill the XSD file with the dataset and XSD is bind with the crystal report. I requires three page header section as in First section I required to write company name and its address and other details and then the two page header sections for the two sub reports. And after that i required to show two sub reports with their individual totals which I am doing in the same individual sub report and after that I required the grand total. I try the solution which you discuss in this articles but unfortunately It gives error "The page size was not large enough to format the contents of an object in the report. Error in File C:\DOCUME~1\kapil\LOCALS~1\Temp\PolicyStatusreport {0E96C854-3FDE-4C82-8A01-C15F9A7138C7}.rpt: Page header or footer longer than a page". My requirement is some what like. Report Header - Page Header 1 Static Data - Page Header 2 Sub Report 2 - Page Header 3 Sub Report 3 Detail Section (Hide it) -------------------------------------------------> After data I required the "Grand Total" which is the sum of the total of the two individual reports. - Page Footer Report Footer Please give me some suggestions to sort out this error.
Khamphouvieng VongphachanhPosted Dec 18, 2008, 8:42 PM
I have read this article, but I still don't know how to display data from multiple tables. In my project i have used more than 2 tables with no relation between them. I want the data from Table(N) displays after the data from Table(N-1) with the same page header (Because the tables have the same column name). So please help me Best Regards Thanks!
Chockalingam MeditedPosted Nov 20, 2007, 12:44 PMEdited Nov 22, 2007, 6:38 AM
Hi Mahesh, When the page loads for the first time it works fine and then when I click the next page it gives the error "The communication channel has not been created" while assigning the datasource for the subreport. (myReportDoc.Subreports[0].SetDataSource(dataSet.Tables[1])). Any idea? I am using Crystal Reports which comes with VS 2005. Thanks Chocks
santosh rauniyareditedPosted Oct 9, 2007, 1:19 AMEdited Oct 15, 2007, 7:41 AM
follow line doesn't work in VS2003 using VB.NET report.Subreports.Item("SubReport1").SetDataSource(ds.Tables("Customers")) Could You tell me how i can use it? I have created object of report like below but still same problem JeyReport jr = new JeyReport(); thanks and regards Santosh
sree deviPosted Sep 11, 2007, 6:33 AM
I am using two subreports.In the local mahcine it's worked well.But in the live machine its showing the error that 'An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) '. What's the solution for this error. Need help immediatly.
RobPosted Aug 20, 2007, 2:14 PM
I'm using CrystalReports .NET 2005 also and don't have Item as a recognized option in the intellisense. I notice the reports I create don't have code-behind .cs files. Could this be the reason for the difference? Can you suggest a workaround? Thanks so much.
RobPosted Aug 20, 2007, 2:00 PM
I'm using CrystalReports .NET 2005 also and don't have Item as a recognized option in the intellisense. I notice the reports I create don't have code-behind .cs files. Could this be the reason for the difference? Can you suggest a workaround? Thanks so much.
RobPosted Aug 20, 2007, 1:41 PM
I'm using CrystalReports .NET 2005 also and don't have Item as a recognized option in the intellisense. I notice the reports I create don't have code-behind .cs files. Could this be the reason for the difference? Can you suggest a workaround? Thanks so much.
JeyaPosted Aug 20, 2007, 6:22 AM
i am using VS 2003 and i could not find the property called Subreports.Item Could You tell me what is the equal command thanks and regards, Jeya.
waelPosted Jul 30, 2007, 2:57 AM
I am using crystal reports 8.5 I have a report that has 2 subreports in it. I placed the subreports under each other but the problem is that when I preview the report, the data from subreport one come over the data from subreport two. If I put the subreports far from each other and subreport one has few records then there will be a big space. Is there a way to solve this dynamically??
Praveen MullangiPosted Jul 23, 2007, 1:42 AM
hai nahesh this is praveen i am geting a problem when i am viewing a crystal report in run time using c# .net field values are repeated please solve the problem as possible as immediately +91 9885348542
David Perales NolePosted Jun 20, 2007, 8:57 AM
Hi. i'm from Peru, your site is very great, i have a problem with the report.. ' cant see the property subreports. i'm using the 9.0 version, can you tell how can i get the upgrade? or i have any other option? thanks.
Kartheek reddy NarayanareddyPosted Jun 20, 2007, 8:05 AM
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.
Mony SathPosted Apr 23, 2007, 7:32 PM
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 [email protected]
JamesPosted Feb 20, 2007, 6:48 PM
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?
Edmond GudezPosted Jan 26, 2007, 4:42 AM
hi.. this is edmond. can i ask other examples...
vikram rajPosted Nov 28, 2006, 1:39 AM
i am using VS 2003 and i could not find the property called Subreports.Item
Mahesh ChandPosted Oct 21, 2006, 11:31 AM
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.
Sarim GhaniPosted Oct 21, 2006, 3:57 AM
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.