So I’m working on a project now that requires the use of using SSRS 2005. The data is based on rules that are in the business layer, so I wanted to use those objects rather than pulling from the database which would most likely require creating a set of tables just for this purpose and all the overhead of writing data temporarily only to pull it right back out again and then delete it. Big waste. Figured it can’t be that hard to use your own IEnumerable<T> data to populate with. I found several articles out there, but the one I based my code off of came from http://www.gotreportviewer.com/ and specifically this code sample. Not difficult when you look at it – quite simple in fact. I’m not going to regurgitate the code here, go read through their site and look through that example. After reading through examples, I like to create my own scratch project and do the code myself to reinforce it.
The one thing that got me was in setting up the DataSource. Looking at the call it, it’s simple. The overload I used is the one that takes in a parameter named “name” of type string and one named “dataSourceValue” of type object. When I see a parameter named “name” it seems to me to be an arbitrary name to be set by the developer. The thing to note is that the name actually has to be the name of the type in the IEnumerable<T>. In my case I have this in a Customer class:
- private List<Product> list;
- public List<Product> GetProducts()
- {
-
- list = new List<Product>();
- list.Add(new Product(1, "Product 1"));
- list.Add(new Product(2, "Product 2"));
- return list;
- }
And in the Page_Load event this code:
- Customer customer = new Customer();
-
- ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
- ReportViewer1.LocalReport.ReportPath = Server.MapPath("Report1.rdlc");
-
- ReportViewer1.LocalReport.DataSources.Add(
- new ReportDataSource("Product", customer.GetProducts()));
-
- ReportViewer1.LocalReport.Refresh();
Initially, since I figured the name property was arbitrary, I used “Products” rather than “Product” and was presented with the following error:
A data source instance has not been supplied for the data source 'Product'. I never found anything that mentioned the importance of the “name” parameter.
Hope this is helpful to someone else out there!