cancel
Showing results for 
Search instead for 
Did you mean: 

Set datasource for subreport

0 Kudos

Hi, all,

I tried to add a subreport to a report document using inProc RAS, like so:

private CrystalDecisions.CrystalReports.Engine.ReportDocument document;

SubreportClientDocument subreportClientDocument;

reportClientDocument = document.ReportClientDocument;

subreportClientDocument = reportClientDocument.SubreportController.ImportSubreport("someName", pathToRPTFile, sectionWhereToAdd);


Later I wanted to set a datasource to the added subreport, but when looping through the main document, I get no subreports found.

.....

CrystalDecisions.CrystalReports.Engine.ReportDocument subReport = document.Subreports[index];

document.Subreports has length of 0, although, I could sware it had worked

Strange thing is, later in the same code I do a loop,

  foreach (CrystalDecisions.CrystalReports.Engine.ReportObject reportObject in document.ReportDefinition.ReportObjects)

            {

                if (reportObject.Kind == ReportObjectKind.SubreportObject)

                {

            and it finds subreportObjects.

What am I doing wrong? And, how can I reach a given subreport, to set a datasource to it?

Thank you, all, in advance!

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Hi Jelena,

Have a look at this sample:

You can find more samples here:

NET RAS SDK Samples - Business Intelligence (BusinessObjects) - SCN Wiki

They are WEB samples also but should work the same in a Windows app

The sample shows:

//First determine the section to add the subreport to - in this case the report header section

boSection = boReportClientDocument.ReportDefController.ReportDefinition.ReportHeaderArea.Sections[0];

//When adding a new subreport (as opposed to importing), you need to leave the ReportURL property blank

boSubreportClientDocument = boReportClientDocument.SubreportController.ImportSubreport("testSub", "", boSection);

//If you try to add a sub-report to a section that is smaller than the dimensions you give the sub-report, you will get an error of

//"Invalid section height" error when you try to add sub-report links to the sub-report database tables.

I'll have to test also.

Set the subreport name to a fully qualified location also.

You can also try using this one:

CrystalDecisions.ReportAppServer.Controllers.ISCRSubreportController.ImportSubreportEx(string, string, CrystalDecisions.ReportAppServer.ReportDefModel.Section, int, int, int, int)

And set the DB info right away as well as the linking etc.

Don

0 Kudos

And it works for me.

CrystalDecisions.ReportAppServer.ReportDefModel.Section rasSection;

CrystalDecisions.ReportAppServer.Controllers.SubreportClientDocument MyNewSub;

                                       

rasSection = rptClientDoc.ReportDefController.ReportDefinition.ReportHeaderArea.Sections[0];

//When adding a new subreport (as opposed to importing), you need to leave the ReportURL property blank

MyNewSub = rptClientDoc.SubreportController.ImportSubreport("World Sales Report", "C:\\Reports\\impsub.rpt", rasSection);

MyNewSub.DatabaseController.LogonEx("ServerName", "Database", "sa", "PW");

To get the links from an existing report, add the subreport and then link it and then you can use this to see what links are set and do the same in the linking API.

foreach (String resultField in rptClientDoc.SubreportController.GetSubreportNames())

{

    textBox1 = resultField.ToString();

    btnReportObjects.Text += "Subreport Name: " + textBox1;

    btnReportObjects.AppendText(":\n");

    SubreportLinks SubLinks = rptClientDoc.SubreportController.GetSubreportLinks(resultField.ToString());

    for (int I = 0; I < SubLinks.Count; I++)

    {

        SubreportLink subLink = SubLinks[I];

        textBox1 = subLink.LinkedParameterName.ToString();

        btnReportObjects.Text += "PM-Name: " + textBox1;

        btnReportObjects.AppendText("\n");

        textBox1 = subLink.MainReportFieldName.ToString();

        btnReportObjects.Text += "Main Field: " + textBox1;

        btnReportObjects.AppendText("\n");

        textBox1 = subLink.SubreportFieldName.ToString();

        btnReportObjects.Text += " Sub Field: " + textBox1;

        btnReportObjects.AppendText(" 'End' \n");

    }

    btnReportObjects.AppendText("\n");

    btnCount.Text = SubLinks.Count.ToString();

}

Don

Answers (0)