This is a migrated thread and some comments may be shown as answers.

Manipulate ReportParameters after Report Construction

3 Answers 264 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ferry
Top achievements
Rank 1
Ferry asked on 04 Jun 2012, 12:58 PM
We are building dynamic reports with various parameters. There is one parameter (X) where each value basically defines what type of parameter should be used next. I figured that the most obvious moment to add or modify parameters is on the NeedDataSource since there aren't that many other hooks. I tried to trick this by writing a class which is called by an ObjectDataSource and only modifies the Report by calling a function that add parameters. This ObjectDataSource is bound to a hidden ReporParameter and has a DataSourceParameter that uses the selection from the first parameter. So once I select a value the method is called that manipulates the ReportParameters (adding, removing, hiding). Unfortunately, any parameter I manipulate or add once the Report is constructed isn't displayed.

The Refresh button on the WebForms ReportViewer however makes later added parameters visible. Is there any way I can trigger the same functionality of the Refresh button programatically?

Right now I have to resort to moving parameters in a custom section on the webpage to get this done. Which I rather not do because it simply looks more inconsistent.

3 Answers, 1 is accepted

Sort by
0
IvanY
Telerik team
answered on 06 Jun 2012, 12:55 PM
Hi Ferry,

Unfortunately there is no way the Report Viewer to be automatically refreshed upon adding a new parameter so as you have guessed you will have to refresh the viewer manually.  So you have to use
this.ReportViewer1.RefreshReport();

whenever a new Parameter is added.

Greetings,
IvanY
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

0
Ferry
Top achievements
Rank 1
answered on 07 Jun 2012, 12:44 PM
I tried that, but that doesn't seem to work when this is triggered through changes in the parameter. Atleast, I figured that that RefreshReport function exists. But to be able to call RefreshReport() on the viewer when a parameter on the Report is changed I needed to do some tricks as described in my initial post. I'll elaborate with the code to make it completely clear what I have done so far.

In short one more time what I try to achieve: When a value for Parameter A is selected I need to add a parameter B of which the actual setup depends on the value selected in A.

1. I created an nested class in the report code that serves as a datasource and has a method that gets the value of A and creates B based on that value. So in fact it is not a datasource but just a way to get a piece of code triggered when a specific parameter is selected. I inlined some code to make the example clearer.
public class ReportLevelDataSource
{
    private ListReport parent;
 
    public ReportLevelDataSource(ListReport parent)
    {
        this.parent = parent;
    }
 
    public IList<string> GetReportLevels()
    {
        return null;
    }
 
    public IList<string> SetReportParameters(int reportLevel)
    {
        if (reportLevel == 1)
        {
            // Add other parameters.
            parent.ReportParameters.Add(new ReportParameter
            {
                Name = StrategyIdParameterName,
                Text = "Strategy ID",
                AllowBlank = false,
                AllowNull = false,
                Visible = true,
                AutoRefresh = true
            });
            parent.OnParametersChanged(EventArgs.Empty);
        }
 
        // Return type is irrelevant, parameter using it is hidden and isn't used.
        return null;
    }
}


1 . I created a bogus trigger parameter that depends on the value of a dropdown selection and uses an Object Datasource.
ReportLevelParameter is A.
var reportLevelObjectDataSource = new ObjectDataSource
{
    DataMember = "SetReportParameters",
    DataSource = reportLevelTriggerDataSource
};
reportLevelObjectDataSource.Parameters.Add(new ObjectDataSourceParameter("reportLevel", typeof(int), "=Parameters.ReportLevelParameter"));
 
var reportLevelTriggerParam = new ReportParameter { Name = "ReportLevelTriggerParameter", Text = "Report type" };
reportLevelTriggerParam.AvailableValues.DataSource = reportLevelObjectDataSource;
reportLevelTriggerParam.AvailableValues.ValueMember = "Id";
reportLevelTriggerParam.AvailableValues.DisplayMember = "Name";
reportLevelTriggerParam.Visible = false;
reportLevelTriggerParam.AutoRefresh = false;
ReportParameters.Add(reportLevelTriggerParam);

3. Subscribe on the event in the webpage and call refresh in the handler.
Report.ParametersChanged += (s, ea) => ReportViewer1.RefreshReport();

Now the throw exception in Visual Studio is:
  Object reference not set to an instance of an object.
   at Telerik.ReportViewer.WebForms.AspNetSessionHelper.SetItem(String key, Object value)
   at Telerik.ReportViewer.WebForms.AspNetSessionHelper.set_StreamManager(StreamManager value)
   at Telerik.ReportViewer.WebForms.AspNetSessionHelper.ClearSession()
   at Telerik.ReportViewer.WebForms.ReportViewer.RefreshReport()

The report itself shows a different stacktrace that probably has to do with the fact that reflection being used:
Object reference not set to an instance of an object.

at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Telerik.Reporting.Processing.Data.ObjectQueryProvider.ResolveDataSource(Boolean evaluateParametersValues)
at Telerik.Reporting.Processing.Data.ObjectQueryProvider.ResolveDataSource()
at Telerik.Reporting.Processing.Data.ObjectDataEnumerable.GetEnumerator()
at Telerik.Reporting.Processing.Data.ResultSet.SeedData(IEnumerable`1 rawData)
at Telerik.Reporting.Processing.Data.ResultSet.Fill(IEnumerable`1 data)
at Telerik.Reporting.Processing.Data.ObjectQueryProvider.Execute(MultidimensionalQuery query)
at Telerik.Reporting.Processing.ParametersManager`1.GetAvailableValuesData(ReportParameterAvailableValues availableValuesDef, ExpressionNode valueExpression, MultidimensionalQueryProvider& provider)
at Telerik.Reporting.Processing.ParametersManager`1.CalculateParameterValues(T parameter, IReportParameter parameterDef, IDictionary`2 parameterValues)
at Telerik.Reporting.Processing.ParametersManager`1.Calculate(T parameter, IDictionary`2 parameterValues)
at Telerik.Reporting.Processing.ParametersManager`1.CalculateChildren(T parameter, IDictionary`2 parameterValues)
at Telerik.Reporting.Processing.ParametersManager`1.Calculate(T parameter, IDictionary`2 parameterValues)
at Telerik.Reporting.Processing.ParametersManager`1.GetParameters(IDictionary`2 parameterValues)
at Telerik.ReportViewer.WebForms.ParametersPage.OnLoadComplete(EventArgs e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
0
IvanY
Telerik team
answered on 11 Jun 2012, 04:59 PM
Hello Ferry,

What you are trying to do can be achieved much easier using external (custom) parameters. The main idea is to add controls outside the Report Viewer and to use them as parameters - show/ hide or create them based on the already selected parameters. After these parameters are set by the user you can pass the values from them to hidden parameters in the report and use the hidden parameters to show the data needed. Generally the best option to execute some code when a parameter changes is to use custom parameters outside the viewer because they will give you full control.

If this does not satisfy your goals it will be best if you describe what are you trying to achieve in more details.

Greetings,
IvanY
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

Tags
General Discussions
Asked by
Ferry
Top achievements
Rank 1
Answers by
IvanY
Telerik team
Ferry
Top achievements
Rank 1
Share this question
or