VB.Net: How to use an Object data source in report (.rdlc)

Basic picture Basic · Sep 15, 2010 · Viewed 17.2k times · Source

My question is similar to this one but I'm having some problems with the actual implementation.

I've got a report (.rdlc) in the business layer of a 3-tier app.

I've got an object in the BL (EmployeeManager) which has a GetEmployees(Expression as Expression(Of Func(Of Employee, Boolean))) As IQueryable(Of Employee) method.

As I didn't want to try and pass a lambda in directly (at least not until I've got something working), I've created a ReportData class in the BL which wraps the GetEmployees() call and exposes the results as an IEnumerable(Of Employee) - which should be very simple. It doesn't even have parameters at the moment.

Ok... So In my report, I've tried to add a new Data Source. I've picked a type of Object and located the ReportData class mentioned above. The wizard completes and adds a DataSources folder to the project inside which is some XML defining a <GenericObjectDataSource> and pointing at the Report class.

ReportData also appears in the Data Sources pane - It has a > next to it but when I expand it, it has no children.

What I don't know how to do is USE the data source - It doesn't seem to expose any methods/members (I haven't even specified that it should call GetEmployees() yet!) and I certainly can't see an IEnumerable(Of Employee) anywhere.

When I try to add a table to the report and it prompts me to select a Dataset, the ReportData Datasource is not shown in the Data source drop-down.

What am I missing? Can someone please point me in the right direction?

My simple ReportData object:

Namespace Reports
    Public Class ReportData

        Private Property EmployeeManager As Interfaces.IEmployeeManager

        Public Sub New()
            ''This sub is here in case it's an instantiation problem - I intend to use dependency injection when I've got this working properly.
            Me.EmployeeManager = New EmployeeManager
        End Sub

        Public Sub New(ByVal EmployeeManager As Interfaces.IEmployeeManager)
            Me.EmployeeManager = EmployeeManager
        End Sub

        Public Function GetEmployees() As IEnumerable(Of Employee)
            Return EmployeeManager.GetEmployees()
        End Function
    End Class
End Namespace

I've also found this which seems to indicate I'm following the correct steps but the properties don't appear as expected

The public properties of the class now appear in the Data Sources window, where they can be dragged and dropped into the report.

This doesn't happen - the properties never appear

EDIT: As pointed out by Alex, I need to use properties not just any methods. Please see Alex Esselfie's answer below for clarification. This still hasn't solved my problem but has got me a step closer...

Answer

Alex Essilfie picture Alex Essilfie · Sep 15, 2010

From your description and accompanying code, there are no properties in the ReportData class. If I reckon well, that class is supposed to be generating the IEnumerable you want to display on the report.

If that's the case, you'll have to select the Employee class as the DataSource in the wizard. This will allow you to display the data on your report.

I'll work on a quick project and add it to this answer in a moment.



Edit 1

How to Bind a Class to a Report

Visual Studio 2008

  1. Open the report in Design View.
  2. Select from the menu bar Data > Show Data Sources
  3. Click Add New Data Source on the Data Sources window.
  4. Select Object and click Next.
  5. Browse the solution tree and select the class you want to bind to.
    In your case, you bind to the Employee class.
  6. Click Next and then Finish.


Visual Studio 2010

  1. Open the report in Design View.
  2. Select from the menu bar View > Report Data
  3. Click New > Dataset... on the Data Sources window.
  4. Enter a name for the dataset (e.g. Employee)
  5. Create a New Data source or select an existing data source.

    Creating a New Data source

    • Select Object and click Next.
    • Browse the solution tree and select the class(es) you want to bind to.
    • Click Finish.


After binding a class to the report, go ahead and design your report as usual.


Edit 2

I uploaded a sample project for you. Here's the link.
If you need further clarification, please notify me so I give you a step by step procedure on how to replicate the project.