Error when executing a static function call

Hello,

The following template syntax throws an error during execution

<<var [commonInfo = CommonInfo]>>

<<var[graphic = MyClass.GetGraphics(“Overview”, <<[commonInfo.Param1]>>, <<[commonInfo.Param2]>>, <<[commonInfo.Param3]>>, <<[commonInfo.Param4]>>, <<[commonInfo.Param5]>>)]>>

I am linking a JSON data source.
a) CommonInfo is a class with five string properties Param1, Param2…
b) GetGraphics is a static function in “MyClass”. I have added it to the known types.

If I pass all strings to the GetGraphics (similar to the first parameter in the call), the call executes successfully. However, for parameters 2 to 6, I am trying to pass from the variable commonInfo.

I get the following error message:

**An error has been encountered at the end of expression 'graphic = MyClass.GetGraphics("Overview", <<['. An expression is expected.**

Please provide pointers on how to pass parameter by deferencing a variable

Regards,

@NaraSg

It appears you are experiencing an issue with passing parameters from a variable in your template syntax. The error message suggests that the expression is not being recognized correctly.

To resolve this, ensure that the syntax for dereferencing the commonInfo variable is accurate. You should use the following format for accessing the properties:

<<[commonInfo.Param1]>>
<<[commonInfo.Param2]>>
<<[commonInfo.Param3]>>
<<[commonInfo.Param4]>>
<<[commonInfo.Param5]>>

Verify that the commonInfo variable is properly initialized and contains the expected values before invoking the GetGraphics method. If the parameters are not being recognized, it may be due to how they are referenced in the expression.

If you continue to encounter issues, consider simplifying the call by passing the parameters directly to GetGraphics to confirm that the method functions as expected. Once confirmed, you can reintroduce the variable references.

For further assistance, please provide additional details about how commonInfo is initialized and any relevant code snippets.

@NaraSg You should use the following syntax:

<<var [commonInfo = CommonInfo]>>

<<var[graphic = MyClass.GetGraphics(“Overview”, commonInfo.Param1, commonInfo.Param2, commonInfo.Param3, commonInfo.Param4, commonInfo.Param5)]>>

Please see the attached template and the following code:

string json = "{ \"CommonInfo\": { \"Param1\": \"First\", \"Param2\": \"Second\", \"Param3\": \"Third\", \"Param4\": \"Fourth\", \"Param5\": \"Fifth\" }}";

JsonDataSource ds = new JsonDataSource(new MemoryStream(Encoding.UTF8.GetBytes(json)));

Document doc = new Document(@"C:\Temp\in.docx");

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(MyClass));
engine.BuildReport(doc, ds, "CommonInfo");

doc.Save(@"C:\Temp\out.docx");
public static class MyClass
{
    public static string GetGraphics(string s0, string s1, string s2, string s3, string s4, string s5)
    {
        return $"{s0}: {s1}, {s2}, {s3}, {s4}, {s5}";
    }
}

out.docx (11.2 KB)
in.docx (14.0 KB)

Thank you very much

Regards,

1 Like

Hello Alexey,
Your sample works very well. But, I have some issues when using it as shown below.

// ===========   start of code ================
using Aspose.Words.Reporting;
using DataObjects;
using System.Text;
using System.Text.Json;
using System.Xml.Linq;
Aspose.Words.License license = new Aspose.Words.License();

long i = 1;
string k = i.ToString();  // long integer converted to string

// Datasources
//string json = "{ \"CommonInfo\": { \"Param1\": \"First\", \"Param2\": \"Second\", \"Param3\": \"Third\", \"Param4\": \"Fourth\", \"Param5\": \"Fifth\", \"Param6\": \"Sixth\" }}";

string json = $$"""
{
    "CommonInfo":
        {
            "Param1": "First",
            "Param2": "Second",
            "Param3": "{{k}}",   // issue here
            "Param4": "Fourth",
            "Param5": "Fifth",
            "Param6": "Sixth"
        }
}
""";
JsonDataSource ds = new JsonDataSource(new MemoryStream(Encoding.UTF8.GetBytes(json)));
Aspose.Words.Document doc = new Aspose.Words.Document("C:\\aspose\\sample - copy\\in.docx");

ReportingEngine engine = new ReportingEngine();
engine.Options = ReportBuildOptions.RemoveEmptyParagraphs;
engine.KnownTypes.Add(typeof(MyClass));
engine.BuildReport(doc, ds, "CommonInfo");
string outputPath = "C:\\aspose\\sample - copy\\out.docx";
doc.Save(outputPath);

// ==========     end of code   ==============

As you can see above, in the json string, I have for Param3, I am assigning a string “k” converted from long type.

When I execute the above code, I get the below error:

An error has been encountered at the end of expression 'graphic = MyClass.GetGraphics("Overview", CommonInfo.Param1, CommonInfo.Param2, CommonInfo.Param3, CommonInfo.Param4, CommonInfo.Param5, CommonInfo.Param6)]>'. Can not resolve method 'GetGraphics' on type 'MyClass'.

Same issue noticed if the json string is as shown below.

string json = "{ \"CommonInfo\": { \"Param1\": \"First\", \"Param2\": \"Second\", \"Param3\": \"1\", \"Param4\": \"Fourth\", \"Param5\": \"Fifth\", \"Param6\": \"Sixth\" }}";

I have uploaded the json view, Please help with some pointers

Regards,

@NaraSg The problem occurs because "Param3" : "1" value is considered by LINQ Reporting Engine as integer. You should use JsonSimpleValueParseMode.Strict to avoid this. Please see the following modified code:

JsonDataLoadOptions options = new JsonDataLoadOptions();
options.SimpleValueParseMode = JsonSimpleValueParseMode.Strict;
JsonDataSource ds = new JsonDataSource(new MemoryStream(Encoding.UTF8.GetBytes(json)), options);

Hello Alexey,

Thanks a lot. It works now.

Regards,

1 Like

Hello Alexey,
When we use multiple embedded templates, it will be difficult to pinpoint parsing errors.
Is there any support in Aspose Word library to log the parsing errors in a file?

Regards,

@NaraSg You can specify ReportBuildOptions.InlineErrorMessages, in this case the engine will write inline template syntax error messages into output documents. If this option is not set, the engine throws an exception when encounters a syntax error.

Thank you very much

1 Like