SAP Knowledge Base Article - Public

2699243 - Can I change the Subreport name and subreport Object name using Crystal reports for Visual Studio .NET SDK

Symptom

We are trying to set the "Object Name" of a subreport using C#. The Name property shows as get; set and accepts the string value we set. But when the report is saved with the C# code and checked in designer, it still shows "Subreport1".

Code Sample below works without any error but when the report is opened using Crystal Reports Designer it shows that the object name is still "Subreport1":

  • CrystalDecisions.CrystalReports.Engine.ReportDocument Mainrd; string RPTPath = @"C:\Main.rpt"; Mainrd.Load(RPTPath);
  • ReportObjectController rptObjCtrl = Mainrd.ReportClientDocument.ReportDefController.ReportObjectController; rptObjCtrl.GetReportObjectsByKind(CrReportObjectKindEnum.crReportObjectKindSubreport)[0].Name = "TestSub";
  • Mainrd.SaveAs(RPTPath);
  • Mainrd.Close();

Environment

Crystal Reports, Developer for Visual Studio

Resolution

There are two properties for each subreport object regarding the name.

In CR Designer right click on the subreport and select "Format Subreport"

The first tab is the Common tab, it has a property called "Object name"

  • This is the object noted in the above code sample.
  • Note: this object is not used anywhere in the report when previewed, it is only the object name when retrieved by the SDK.
  • This property has a get/set value

The second Tab labelled "Subreport", it has a property called "Subreport name"

  • This the name of the subreport used when it was created or imported less the rpt extension
  • It is the name of the TAB when you preview the subreport

The API to get this values is:

foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject rptObj1 in rptObjs)
{
    switch (rptObj1.Kind)
    {
        // look for sub report object and display info
        case CrReportObjectKindEnum.crReportObjectKindSubreport:
            CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject subObj1;
            try
            {
                subObj1 = (CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject)rptObj1;
                subObj1.SubreportName.toString();

 Using the Object Browser in Visual Studio you will see this property only has a get, thus making it non-changable.

The only way to set the name in code is to use the .Add() method by opening a report from the file system as a new object.

Example to add multiple subreports to a section:

  • CrystalDecisions.ReportAppServer.ReportDefModel.Section rasSection;
    CrystalDecisions.ReportAppServer.Controllers.SubreportClientDocument MyNewSub;
  • // this add subs to area
    for (int gg = 0; gg < 2; gg++)
    {
        rasSection = rptClientDoc.ReportDefController.ReportDefinition.ReportFooterArea.Sections[0];
  •     //When adding a new subreport (as opposed to importing), you need to leave the ReportURL property blank
        MyNewSub = rptClientDoc.SubreportController.ImportSubreportEx("DonSubReportName" + gg.ToString(), @"c:\reports\Formulas.rpt", rasSection, 100, 100 + (gg * 1100), 11520, 1000);
    }

What this does is in a loop it creates a subreport name "DonSubReportName" and appends a number to the name because you cannot add a subreport with the same name into the report.

When you preview or look at the Subreport tab and the name this is what you will see, the Object Name by default gets the same value.

If you attempt to Clone() the subreport object and change the name for example this way:

  • foreach (CrystalDecisions.ReportAppServer.ReportDefModel.ReportObject rptObj1 in rptObjs)
    {
        switch (rptObj1.Kind)
        {
            // look for sub report object and display info
            case CrReportObjectKindEnum.crReportObjectKindSubreport:
                CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject subObj1;
                try
                {
                    subObj1 = (CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject)rptObj1;
                    subObj1.SubreportName.ToString();
                    ++flcnt;
  •                 SubreportController subreportController = rptClientDoc.SubreportController;
                    SubreportClientDocument subreportClientDocument = subreportController.GetSubreport(subObj1.SubreportName);
  •                 // Subname is read only
                    //ReportObjectController rptObjCtrl = rptClientDoc.ReportDefController.ReportObjectController;
                    //rptObjCtrl.GetReportObjectsByKind(CrReportObjectKindEnum.crReportObjectKindSubreport)[0].Name = "DonSubReportName0";
  •                 CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject objSubreport2 = (CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject)subObj1.Clone(true);
                    //add a subreport
                    CrystalDecisions.ReportAppServer.ReportDefModel.Section rasSection;
  •                 rasSection = rptClientDoc.ReportDefController.ReportDefinition.ReportFooterArea.Sections[0];
                    rptClientDoc.ReportDefController.ReportObjectController.Remove(subObj1);
  •                 objSubreport2.Name = "DonTest";
                    objSubreport2.SubreportName = "SubreportObject1";
                    rptClientDoc.ReportDefController.ReportObjectController.Add(objSubreport2, rasSection, -1);

Will gneerate this error:

Error in : Adding or changing this kind of report object is not supported.

Keywords

cr for vs, subreport name , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , Problem

Product

SAP Crystal Reports, version for Visual Studio .NET 2005