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

How to pass a parameter to a report using typeResourceReport

1 Answer 316 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Aubrey Ivan
Top achievements
Rank 1
Aubrey Ivan asked on 31 Oct 2019, 03:55 AM

I am trying to pass a model to the report using typeReportResource to set it as its dataSource, am I doing this wrong?

This is my Controller 

    public class ReportViewerController : Controller
    {
        private IOrganisationLogic _organisationLogic;
        public ReportViewerController(IOrganisationLogic organisationLogic)
        {
            _organisationLogic = organisationLogic;
        }
        public ActionResult Index()
        {
            var organisation = _organisationLogic.GetOrganisation();            

            var typeReportSource = new TypeReportSource();
            typeReportSource.TypeName = typeof(OrganisationSample).AssemblyQualifiedName;

            typeReportSource.Parameters.Add(new Parameter("flag", true));
            typeReportSource.Parameters.Add(new Parameter("organisation", organisation));

            return View(typeReportSource);
        }
    }

 

And here is the report design that i've created...


    public partial class OrganisationSample : Telerik.Reporting.Report
    {
        private IOrganisationLogic _organisationLogic;
        public OrganisationSample(bool flag, OrganisationLogic organisation)
        {
            //
            // Required for telerik Reporting designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

            if (flag)
            {
                this.DataSource = organisation;
            }
        }
    }


what is the best approach I could do? Iwant to set the objectDataSource dynamically, each report has its own objectDataSource and I just want a single viewer that can use render multiple reports.

1 Answer, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 04 Nov 2019, 01:38 PM

Hi Aubrey Ivan,

It is not technically possible to pass arguments to the report constructor using a TypeReportSource. The parameter values in the TypeReportSource.Parameters collections are applied to the Report.Parameters, not the report's constructor arguments.

We recommend that you set up a single ObjectDataSource component in the report. Then, modify the data retrieval using a data source parameter for each case.

Regards,
Nasko
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Lauren
Top achievements
Rank 1
commented on 20 Jul 2022, 09:44 AM

Hi Nasko, Still I want few param to constructor not as argument then what would be the best way to have the properties to constructor I have many condition that can be possible to constructor
Todor
Telerik team
commented on 25 Jul 2022, 08:05 AM

Hi Lauren,

The reports that inherit from Telerik.Reporting.Report class may have multiple constructors with a different number of parameters. If you use TypeReportSource, our code will try to instantiate the report with reflection, through the Activator.CreateInstance Method and will use the parameterless constructor.

If you want to use an alternative report constructor, you may pass the report wrapped in an InstanceReportSource instead. Its ReportDocument property may hold the report instance. For the viewers with the REST Service, you will have to create also a custom ReportSource Resolver that would resolve the client-side reportSource passed to the service to a valid server-side InstanceReportSource.

Tags
General Discussions
Asked by
Aubrey Ivan
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Share this question
or