Using Parameters with the EntityDataSource Component
This section discusses more in-depth how to pass parameters to a method of the ObjectContext/DbContext with the EntityDataSource
component. The provided examples and code snippets assume an existing Entity Data Model of the Adventure Works sample database with the following structure:
The EntityDataSource 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 EntityDataSource
component can call a method of the ObjectContext/DbContext 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 ContextMember
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 EntityDataSource
component will raise an exception at runtime. The following code snippet illustrates how to pass parameters to the previous method programmatically:
var entityDataSource = new Telerik.Reporting.EntityDataSource();
entityDataSource.Context = typeof(AdventureWorksEntities);
entityDataSource.ContextMember = "GetProducts";
entityDataSource.Parameters.Add("color", typeof(string), "Black");
entityDataSource.Parameters.Add("price", typeof(decimal), 100);
var report = new Report1();
report.DataSource = entityDataSource;
Dim entityDataSource As New Telerik.Reporting.EntityDataSource()
entityDataSource.Context = GetType(AdventureWorksEntities)
entityDataSource.ContextMember = "GetProducts"
entityDataSource.Parameters.Add("color", GetType(String), "Black")
entityDataSource.Parameters.Add("price", GetType(Decimal), 100)
Dim report As New Report1()
report.DataSource = entityDataSource
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 entityDataSource = new Telerik.Reporting.EntityDataSource();
entityDataSource.Context = typeof(AdventureWorksEntities);
entityDataSource.ContextMember = "GetProducts";
entityDataSource.Parameters.Add("color", typeof(string), "=Parameters.Color.Value");
entityDataSource.Parameters.Add("price", typeof(decimal), "=Parameters.Price.Value");
var report = new Report1();
report.DataSource = entityDataSource;
report.ReportParameters.Add("Color", Telerik.Reporting.ReportParameterType.String, "Black");
report.ReportParameters.Add("Price", Telerik.Reporting.ReportParameterType.Float, 100);
Dim entityDataSource As New Telerik.Reporting.EntityDataSource()
entityDataSource.Context = GetType(AdventureWorksEntities)
entityDataSource.ContextMember = "GetProducts"
entityDataSource.Parameters.Add("color", GetType(String), "=Parameters.Color.Value")
entityDataSource.Parameters.Add("price", GetType(Decimal), "=Parameters.Price.Value")
Dim report As New Report1()
report.DataSource = entityDataSource
report.ReportParameters.Add("Color", Telerik.Reporting.ReportParameterType.String, "Black")
report.ReportParameters.Add("Price", Telerik.Reporting.ReportParameterType.Float, 100)