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:
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();
}
}
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 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;
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
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);
Dim openAccessDataSource As 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 New Report1()
report.DataSource = openAccessDataSource
report.ReportParameters.Add("Color", Telerik.Reporting.ReportParameterType.String, "Black")
report.ReportParameters.Add("Price", Telerik.Reporting.ReportParameterType.Float, 100)