New to Telerik Reporting? Download free 30-day trial

Connecting to a Telerik Data Access Model with the OpenAccessDataSource Component

This section discusses how to connect the OpenAccessDataSource component to a Telerik Data Access Model. The provided examples and code snippets assume an existing Telerik Data Access Model of the Adventure Works sample database with the following structure:

The structure of the Telerik Data Access Model of the Adventure Works sample database we are going to use in the examples

The simplest way to configure OpenAccessDataSource in Report Designer is to use the OpenAccessDataSource Wizard. That wizard is started automatically when you create a new OpenAccessDataSource, but you can invoke it manually at any time from the context menu associated with the data source by choosing "Configure":

The context menu of the OpenAccessDataSource component with the Configure option chosen

To configure the OpenAccessDataSource component programmatically you need to specify at least an ObjectContext and a property or a method from that ObjectContext which is responsible for data retrieval. Assign the type of the OpenAccessContext to the ObjectContext property of OpenAccessDataSource and the name of the desired member to the ObjectContextMember property, as shown in the following example:

var openAccessDataSource = new Telerik.Reporting.OpenAccessDataSource();

openAccessDataSource.ObjectContext = typeof(AdventureWorksEntities);
openAccessDataSource.ObjectContextMember = "Products";

var report = new Report1();

report.DataSource = openAccessDataSource;
Dim openAccessDataSource As New Telerik.Reporting.OpenAccessDataSource()

openAccessDataSource.ObjectContext = GetType(AdventureWorksEntities)
openAccessDataSource.ObjectContextMember = "Products"

Dim report As New Report1()

report.DataSource = openAccessDataSource

The above code snippet connects the OpenAccessDataSource component to the AdventureWorksEntities context and retrieves the information for all products from the Products auto-generated property. Instead of specifying a type you can assign a live instance of the OpenAccessContext. In this case however it is your responsibility to destroy that OpenAccessContext instance when done with the report:

var openAccessDataSource = new Telerik.Reporting.OpenAccessDataSource();
var openAccessContext = new AdventureWorksEntities();

openAccessDataSource.ObjectContext = openAccessContext;
openAccessDataSource.ObjectContextMember = "Products";

var report = new Report1();

report.DataSource = openAccessDataSource;

// You have to dispose the OpenAccessContext explicitly when done with the report.
openAccessContext.Dispose();
Dim openAccessDataSource As Telerik.Reporting.OpenAccessDataSource = New Telerik.Reporting.OpenAccessDataSource()
Dim openAccessContext As AdventureWorksEntities = New AdventureWorksEntities()

openAccessDataSource.ObjectContext = openAccessContext
openAccessDataSource.ObjectContextMember = "Products"

Dim report As New Report1()

report.DataSource = openAccessDataSource

' You have to dispose the OpenAccessContext explicitly when done with the report.
openAccessContext.Dispose()

Binding to a method is more flexible than binding to a property, because it is possible to execute some custom business logic when retrieving data for the report. If the specified method has arguments, the OpenAccessDataSource component allows you to pass parameters to those arguments via the Parameters collection. For example, let us extend the AdventureWorksEntities context using a partial class that defines the following method:

partial class AdventureWorksEntities
{
    public System.Collections.Generic.List<Product> GetProducts(string color, decimal price)
    {
        return this.Products.Where(product => product.Color == color && product.ListPrice <= price).ToList();
    }
}
Partial Class AdventureWorksEntities
    Public Function GetProducts(ByVal color As String, ByVal price As Decimal) As System.Collections.Generic.List(Of Product)
        Return Me.Products.Where(Function(product) product.Color = color And product.ListPrice <= price).ToList()
    End Function
End Class

You can bind the OpenAccessDataSource component to that method with the following code snippet:

var openAccessDataSource = new Telerik.Reporting.OpenAccessDataSource();

openAccessDataSource.ObjectContext = typeof(AdventureWorksEntities);
openAccessDataSource.ObjectContextMember = "GetProducts";
openAccessDataSource.Parameters.Add("color", typeof(string), "Black");
openAccessDataSource.Parameters.Add("price", typeof(decimal), 100);

var report = new Report1();

report.DataSource = openAccessDataSource;
Dim openAccessDataSource As New Telerik.Reporting.OpenAccessDataSource()

openAccessDataSource.ObjectContext = GetType(AdventureWorksEntities)
openAccessDataSource.ObjectContextMember = "GetProducts"
openAccessDataSource.Parameters.Add("color", GetType(String), "Black")
openAccessDataSource.Parameters.Add("price", GetType(Decimal), 100)

Dim report As New Report1()

report.DataSource = openAccessDataSource

The names and types of the parameters in the Parameters collection should match exactly the names and types of the method arguments. In case this requirement is not fulfilled the OpenAccessDataSource component will not be able to resolve or call correctly the method and will raise an exception at runtime.

In this article