ReportXmlSerializer: System.ArgumentException: 'Invalid name character in 'MyData[]'. The '[' character, hexadecimal value 0x5B, cannot be included in a name.'

1 Answer 116 Views
Serialization
Kay
Top achievements
Rank 1
Kay asked on 16 Apr 2023, 07:36 AM

ReportXmlSerializer().Serialize() throws unexpected System.ArgumentException:
'Invalid name character in 'MyData[]'. The '[' character, hexadecimal value 0x5B, cannot be included in a name.'

How can I prevent this?
- Or -
What other way can I save the data with the report?
private void CreateReport() {
    var report = new Telerik.Reporting.Report();
    report.Name = "MyReport";
    var detail = new Telerik.Reporting.DetailSection();
    report.Items.Add(detail);
    var textBox = new Telerik.Reporting.TextBox();
    textBox.Value = "Hello World!";
    detail.Items.Add(textBox);

    var dataSource = new ObjectDataSource();
    dataSource.Name = "MyData";
    dataSource.DataSource = MyData.Sample;

    report.DataSource = dataSource;

    var reportPath = @"C:\develop\Temp\MyReport.trdp";
    new ReportXmlSerializer().Serialize(reportPath, report);
}

public class MyData {
    public static readonly MyData[] Sample = { new() { Text = "Foo", Number = 42 }, new() { Text = "Bar", Number = 21 } };

    public string Text { get; set; }
    public int Number { get; set; }
}

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 19 Apr 2023, 03:03 PM

Hello Kay,

Thank you for the provided information regarding the issue!

The ObjectDataSource component itself is not meant to hold the data for the report within the report. Instead, you can think of it as a set of instructions for the Reporting engine to follow at runtime in order to retrieve the data. 

For example, the ObjectDataSource.DataSource is the type of the class that has the code which will be invoked for the data to be retrieved. The ObjectDataSource.DataMember itself represents the name of the method that will be invoked from the given class. All of this happens at runtime through System.Reflection.

Considering the above, if you wish for the data itself to be embedded in the report, I would advise serializing it to a JSON and saving it in the report with the JsonDataSource component. Still, if you wish to make the above code compile without exception, you may change it to the following:

            var report = new Telerik.Reporting.Report();
            report.Name = "MyReport";
            var detail = new Telerik.Reporting.DetailSection();
            report.Items.Add(detail);
            var textBox1 = new Telerik.Reporting.TextBox();
            // textBox1
            textBox1.Location = new Telerik.Reporting.Drawing.PointU(new Telerik.Reporting.Drawing.Unit(0, Telerik.Reporting.Drawing.UnitType.Cm), new Telerik.Reporting.Drawing.Unit(0, Telerik.Reporting.Drawing.UnitType.Cm));
            textBox1.Name = "NameDataTextBox";
            textBox1.Size = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(5.0, Telerik.Reporting.Drawing.UnitType.Cm), new Telerik.Reporting.Drawing.Unit(0.6, Telerik.Reporting.Drawing.UnitType.Cm));
            textBox1.Style.BorderStyle.Default = Telerik.Reporting.Drawing.BorderType.Solid;
            textBox1.StyleName = "Data";
            textBox1.Value = "Hello World!";
            detail.Items.AddRange(new Telerik.Reporting.ReportItemBase[] { textBox1 });

            var dataSource = new ObjectDataSource();
            dataSource.Name = "MyData";
            dataSource.DataMember = "Sample";
            dataSource.DataSource = typeof(MyData);


            report.DataSource = dataSource;
            var reportPath = @"C:\temp\MyReport.trdx";
            new ReportXmlSerializer().Serialize(reportPath, report);

When you pass the data to the report at runtime, you can directly assign the data to the DataSource of the report or the data item, which means you could do the following:

report.DataSource = MyData.Sample;

Then you can use this report in your application via the InstanceReportSource class but when you need to save the report to TRDP/TRDX, and the data must be embedded, you have to first serialize it. For details on how the Instance and other report sources work, as well as how to use them, please refer to the following articles:

I hope that the provided information will help, please let me know if you have additional issues/questions.

Regards,
Dimitar
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Tags
Serialization
Asked by
Kay
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or