cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Change subreport properties using .NET SDK

HML
Explorer
0 Likes
903

Hello,
I'm using Crystal Reports SDK for semi-automated report customizing and I am currently facing a very strange issue:

As soon as I change a property of a subreport (e.g. background color, position) Crystal Reports throws an InternalException "Invalid field name" for some reports when calling ReportDocument.Save or ReportDocument.Refresh. Even if I start printing or preview after the change, I get the same error.

I created two almost identical reports (let's say testreport_01 and testreport_02) for testing. Both reports contain a subreport and a formula field that is linked to a parameter in the subreport.

The only difference: The parameter in testreport_01 was created manually before linking and in testreport_02 the parameter field was created automatically when the subreport link was created.

HML_0-1714718483039.png

HML_1-1714718500689.png

testreport_01.rpt passes the following test, testreport_02.rpt turns red.

 

using CrystalDecisions.CrystalReports.Engine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing;

namespace ChangeSubreportTestProject
{
    [TestClass]
    public class ChangeSubreportTests
    {
        [TestMethod]
        [DataRow("./testdata/testreport_01.rpt")]
        [DataRow("./testdata/testreport_02.rpt")]
        public void TestChangeBackgroudColor(string filename)
        {
            var reportDocument = new ReportDocument();
            reportDocument.Load(filename);

            var reportObjects = reportDocument.ReportDefinition.ReportObjects;

            foreach (ReportObject reportObject in reportObjects)
            {
                if (reportObject is SubreportObject)
                    reportObject.Border.BackgroundColor = Color.Red;
            }

            reportDocument.SaveAs(filename + ".customized.rpt");

            reportDocument.Close();
            reportDocument.Dispose();
        }
    }
}

 

What I also found out so far:
1. The problem does not occur if the check "Select data in subreport based on field" is set when linking.
2. If I catch the exception when saving and call ReportDocument.SaveAs again, the report is saved, but the parameter field has disappeared.

Is there any way to avoid this problem without manually replacing the parameter fields in the affected reports?
Or if not, how can I at least identify the affected parameters?

I'm using the latest runtime version 13.0.34 resp. 13.0.35

Accepted Solutions (1)

Accepted Solutions (1)

DonWilliams
Active Contributor
0 Likes

Any subreport parameter beginning with ?PM- are internal objects for CR to use only. Don't play with them.

Create your own subreport parameter and use those for editing etc.

The error is due to the field linked no longer exists.

HML
Explorer
0 Likes
Hello Don, for more than 25 years the names of all my parameters have started with Pm-. Is it documented somewhere that I am not allowed to name them like that? And I'm not playing around with the parameters here, in the example code I'm just changing the background color of the subreport. I suspect the problem is that the API does not change the properties of the existing subreport instance but creates a clone, deletes the original and reinserts the clone. This destroys the subreport links. Best regards HML
DonWilliams
Active Contributor
0 Likes
I don't recall any actual stated inof not to use CR internal Parameter or use the same name. You will always see ?Pm- when a parameter is added. What is in your formula you are using? If the formula is invalid that could be why CR is deleting it, bad field name etc...

Answers (0)