views:

1295

answers:

1

I am attempting to get nested objects to work in Microsoft reports. I downloaded example code from http://www.gotreportviewer.com/objectdatasources/index.html, and it runs correctly.

I built the following little app based on a Windows Form and their code, and all that I ever get when I reference a nested object value is a "#Error" in the place where the data should appear.

In the report, I use the same nested object syntax that is recommended at the web site:

=Fields!Name.Value.FirstName

It works for their application on my computer, but not for mine. I can't understand it! Has anyone encountered this, or know why this happens?

Furthermore - and I don't know if this is related - I can't add a single instance of ClientItem to the LocalReport.DataSources object. It has to be a list. However, when it renders, it only shows one row of (#Errored) data in the table on the report.

Any help would be appreciated!

namespace ReportTest
{

    public class ClientItem
    {
        public int Id { get; set; }
        public ClientName Name { get; set; }
    }

    public class ClientName
    {
        public ClientName(string first, string last)
        {
            FirstName = first;
            LastName = last;
        }

        string FirstName { get; set; }
        string LastName { get; set; }
    }

    public partial class Form1 : Form
    {
        private List<ClientItem> clients = new List<ClientItem>();

        public Form1()
        {
            InitializeComponent();
            PopulateLists();
            GenerateReport();
        }

        private void PopulateLists()
        {
            clients.Add(new ClientItem { Id = 1, Name = new ClientName("Adrian", "Adesco") } );
            clients.Add(new ClientItem { Id = 2, Name = new ClientName("Brian", "Briar") } );
            clients.Add(new ClientItem { Id = 3, Name = new ClientName("Clive", "Cussler") } );
        }

        private void GenerateReport()
        {
            this.Text = "Report Control Demo";
            this.ClientSize = new System.Drawing.Size(950, 600);

            ReportViewer reportViewer = new ReportViewer();

            reportViewer.ProcessingMode = ProcessingMode.Local;

            reportViewer.LocalReport.ReportPath = "TestReport.rdlc";

            reportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportTest_ClientItem", clients));

            reportViewer.Dock = DockStyle.Fill;
            this.Controls.Add(reportViewer);

            reportViewer.RefreshReport();
        }
    }
}
A: 

Okay, the solution for the problem with the test case (above) is to make the properties in ClientName public:

public class ClientName
{
    public ClientName(string first, string last)
    {
        FirstName = first;
        LastName = last;
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

This solved the test case problem for me.

However, I was still having problems with my actual report. It was still giving an error. As it turned out, this was because the sub-object was actually defined in a different assembly.

In order for it to work, the following line had to be added to the AssemblyInfo.cs file of the project containing the sub-object:

[assembly: AllowPartiallyTrustedCallers]

Now it works! It took quite a while to discover that - hope this helps someone...

Reiste