How to: Use Parameters with the OpenAccessDataSource Component

This topic discusses 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 Product schema from the Adventure Works sample database.

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's 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

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 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 programmatically pass parameters to the previous method:

Telerik.Reporting.OpenAccessDataSource 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);
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = openAccessDataSource;
Dim openAccessDataSource As Telerik.Reporting.OpenAccessDataSource = 
    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 Telerik.Reporting.Report = New Telerik.Reporting.Report()
report.DataSource = openAccessDataSource

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

Telerik.Reporting.OpenAccessDataSource 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");
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = openAccessDataSource;
report.ReportParameters.Add("Color", Telerik.Reporting.ReportParameterType.String, "Black");
report.ReportParameters.Add("Price", Telerik.Reporting.ReportParameterType.Float, 100);
Dim openAccessDataSource As Telerik.Reporting.OpenAccessDataSource = 
    New Telerik.Reporting.OpenAccessDataSource()
openAccessDataSource.ObjectContext = GetType(AdventureWorksEntities)
openAccessDataSource.ObjectContextMember = "GetProducts"
openAccessDataSource.Parameters.Add("color", GetType(String), "=Parameters.Color.Value")
openAccessDataSource.Parameters.Add("price", GetType(Decimal), "=Parameters.Price.Value")
Dim report As Telerik.Reporting.Report = New Telerik.Reporting.Report()
report.DataSource = openAccessDataSource
report.ReportParameters.Add("Color", Telerik.Reporting.ReportParameterType.String, "Black")
report.ReportParameters.Add("Price", Telerik.Reporting.ReportParameterType.Float, 100)

See Also