DataSource for Crystal sub report vb.net

CraigLazar
CraigLazar used Ask the Experts™
on
Hi, I have written an app that is using crystal reports. I am using vs2008, and i have an rpt file with an embedded report. How do i set the source for my sub report?
I am currently using this method for the source of the main report when i run the report from my vb app

            Dim adap As New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            Try
                adap.Fill(ds)

                'Assign the datasource and set the properties for Report viewer
                Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                rptDocument.Load(AppPath & "\Reports\" & GReport)
                rptDocument.SetDataSource(ds.Tables(0))
                rptDocument.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, AppPath & "\DSDailyBackup_Report.pdf")

Which works really well, what i have done now is i have added a subreport that has a different data source in the Page footer of the main report, i am not sure how then to set the datasource for the sub report

thanks allot
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
It works similarly but you have to find the subreport object in the report and assign a data source to it.

It is something like (syntax is not correct.)

ForAll rptObjects in rpt.Document.Obejcts
    If rptObject.type = subreportobject then
         set data source

mlmcc
Developer
Commented:
You can use the following code, which also allows you to have more than one named subreport in the main report

 Dim osub1 As ReportDocument = rptDocument.OpenSubreport("NameofYourFirstSubReport")
 osub1.SetDataSource(ds1)
 Dim osub2 As ReportDocument = rptDocument.OpenSubreport("NameofYourSecondSubReport")
 osub2.SetDataSource(ds2)


where the datasources for your subreports are ds1 and ds2 (datasets or datatables)

Author

Commented:
Hi vbgb,

ok thanks for the code, i adjusted my code to look like this
DS.Tables("1") is the source of the main report
DS.Tables("2") is the source for my sub report
In run time i checked and there is data in each table

             Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
            rptDocument.Load(strReportPath)
            rptDocument.SetDataSource(DS.Tables("1"))
            Dim osub1 As ReportDocument = rptDocument.OpenSubreport("rptBE_DBSR.rpt")
            osub1.SetDataSource(DS.Tables("2"))

The problem is that my sub report is empty

thanks for the help
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Geoff BryanDeveloper

Commented:
Hi Craig

I think you need to change the name of the sub report on the 'Dim oSub1' line to be the internal name of the sub-report that is stored in the main report, rather than a 'rpt' filename.

In your main report the sub-report object will have a property called 'SubreportName' - it is the value of this property that needs to be passed to  the rptDocument.OpenSubReport method.

Author

Commented:
Hi vbgb,
Ok i got the sub report to load using the following
            Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument

            rptDocument.Load(strReportPath)
            rptDocument.SetDataSource(DS.Tables("1"))
            rptDocument.OpenSubreport("rptTest1").SetDataSource(DS.Tables("2"))

But for some reason my graph on the sub report is not loading

thanks
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Commented:
Is this on the development machine or a client?

mlmcc

Author

Commented:
on my development machine, i have the report open in VS2008, and its a formula that i have created which then i want to put onto my report
Geoff BryanDeveloper

Commented:
Are you saying that you have now successfully set the datasource for the sub-report, and you are getting some data shown in the sub report - which was your original question.

The problem with your graph not loading  is probably a different issue, which I am not sure of the answer to I'm afraid.

Author

Commented:
Hi vbgb
yip i got the data coming thru on the sub report - but my graph is not showing..

thanks for the help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial