Progress® Telerik® Reporting R2 2017

Using parameters with the OpenAccessDataSource component

This section discusses more in-depth how to pass parameters to a method of the OpenAccessContext with the OpenAccessDataSource component. The provided examples and code snippets assume an existing Telerik Data Access Model of the Adventure Works sample database with the following structure:

Note Note

The OpenAccessDataSource Wizard can detect parameters of the data-retrieval method, and it will ask you to provide values for them at Configure Data Source Parameters step.

The OpenAccessDataSource component can call a method of the OpenAccessContext based on the name of the method, and additionally based on the arguments which make the signature of that method. 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();
    }
}

To call the above method specify its name to the ObjectContextMember property and define a data source parameter in the Parameters collection for each method argument. The names and types of the data source parameters must match exactly the names and types of the corresponding method arguments otherwise the OpenAccessDataSource component will raise an exception at runtime. The following code snippet illustrates how to pass parameters to the previous method programmatically:

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;

When declaring a data source parameter you can specify a default value for that parameter and the value will be passed automatically to the corresponding method argument. Instead of supplying the parameter value directly, you can specify an expression to be evaluated at runtime. For example, this way it is possible to link the data source parameter to an existing report parameter, as shown in the following code snippet:

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

openAccessDataSource.ObjectContext = typeof(AdventureWorksEntities);
openAccessDataSource.ObjectContextMember = "GetProducts";
openAccessDataSource.Parameters.Add("color", typeof(string), "=Parameters.Color.Value");
openAccessDataSource.Parameters.Add("price", typeof(decimal), "=Parameters.Price.Value");

var report = new Report1();

report.DataSource = openAccessDataSource;
report.ReportParameters.Add("Color", Telerik.Reporting.ReportParameterType.String, "Black");
report.ReportParameters.Add("Price", Telerik.Reporting.ReportParameterType.Float, 100);