cancel
Showing results for 
Search instead for 
Did you mean: 

How to Rename subreport's object name?

nikhil_sabnis2
Active Participant
0 Kudos
703

Hi

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 fine 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();

We are using:

Runtime: "13.0.20.2399"

Visual Studio 2017

Are we doing something wrong here? Please let us know. Appreciate your help!

Regards

Nikhil

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

Something like htis:

// add a subreport
CrystalDecisions.ReportAppServer.ReportDefModel.Section rasSection;
CrystalDecisions.ReportAppServer.Controllers.SubreportClientDocument MyNewSub; // = new SubreportClientDocument();

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", "d:\\atest\\soda\\impsub.rpt", rasSection);
MyNewSub.DatabaseController.LogonEx("YourServer", "xtreme", "sa", "Password");


//Change an existing subreport
CrystalDecisions.CrystalReports.Engine.ReportObjects crReportObjects;

//set the crSections object to the current report's sections
CrystalDecisions.CrystalReports.Engine.Sections crSections = rpt.ReportDefinition.Sections;

if (resultField.ToString() == "World Sales Report.rpt")
{
    //loop through all the sections to find all the report objects
    foreach (CrystalDecisions.CrystalReports.Engine.Section crSection in crSections)
    {
        crReportObjects = crSection.ReportObjects;
        //loop through all the report objects to find all the subreports
        foreach (CrystalDecisions.CrystalReports.Engine.ReportObject crReportObject in crReportObjects)
        {
            if (crReportObject.Kind == ReportObjectKind.SubreportObject)
            {
                CrystalDecisions.ReportAppServer.ReportDefModel.Section rasSection;
                rasSection = rptClientDoc.ReportDefController.ReportDefinition.FindSectionByName(crSection.Name);
                CrystalDecisions.ReportAppServer.Controllers.SubreportClientDocument MyNewSub;
                MyNewSub = rptClientDoc.SubreportController.GetSubreport(resultField.ToString());

                CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject objSubreport = rptClientDoc.ReportDefController.ReportObjectController.GetAllReportObjects()[crReportObject.Name] as CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject;
                CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject objSubreport2 = (CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject)objSubreport.Clone(true);
                rptClientDoc.ReportDefController.ReportObjectController.Remove(objSubreport);
                CrystalDecisions.ReportAppServer.ReportDefModel.SubreportLinks mySubLinks = rptClientDoc.SubreportController.GetSubreportLinks(objSubreport2.SubreportName.ToString());
                mySubLinks.RemoveAll();

                objSubreport2.Left = 10;
                objSubreport2.Height = 10;
                objSubreport2.Width = 10;
                objSubreport2.Name = "sub1";
                objSubreport2.SubreportName = "sub1";
                //rptClientDoc.ReportDefController.ReportObjectController.Modify(rptClientDoc.ReportDefController.ReportObjectController.GetAllReportObjects()[crReportObject.Name] as CrystalDecisions.ReportAppServer.ReportDefModel.SubreportObject, objSubreport2);
                rptClientDoc.ReportDefController.ReportObjectController.Add(objSubreport2, rasSection, -1);
            }
        }
    }
}

Answers (1)

Answers (1)

0 Kudos

To be able to change the name normally you would use RAS and it's a read only property:

string Name { get; }
Member of CrystalDecisions.ReportAppServer.Controllers.ISCRSubreportClientDocument

What you would have to do is Clone the subreport and change it's name and then delete the old one and add the cloned object back into the report.

Don

nikhil_sabnis2
Active Participant
0 Kudos

Hi Don

Could you please help me with a sample?

Regards

Nikhil