Report parameter in report constructor

11 posts, 0 answers
  1. Rui
    Rui avatar
    4 posts
    Member since:
    Mar 2014

    Posted 19 Aug 2014 Link to this post

    Hi,

    I'm currently working on a ASP.NET MVC application in which we recently start integrating with Telerik Report.

    I designed the report as shown in report-design.png. I fed the object datasource at runtime and it works just fine with the following code. And it shows in view as in report-view.png.

    /// <summary>
    /// Summary description for Report2.
    /// </summary>
    public partial class TelerikQuoteReport : Telerik.Reporting.Report
    {
        public TelerikQuoteReport() //WISH IT TO ACCEPT PARAMETER LIKE ID
        {
            InitializeComponent();

            var appCon = new AppContext(new DbContext());
            var rep = new OperationalDocumentRepository(appCon);

            //WISH TO REPLACE THE ID WITH PARAMETER SO I CAN REUSE THE REPORT WITH DIFFERENT DATA
            this.DataSource = new List<ODQuote>() { rep.GetDocument<ODQuote>(new Guid("968e62ac-b295-4fd1-b93b-7fdc99d03093")) };
        }
    }

    Except now I want to dynamically feed the ID so I can reuse the report with different data. I tried the following approach and tried to feed a parameter to the report constructor, but without any luck.

    $(document).ready(function () {
            $("#reportViewer1").telerik_ReportViewer({
                serviceUrl: "/api/reports",
                templateUrl: "/ReportViewer/templates/telerikReportViewerTemplate-8.1.14.804.html",
                reportSource: {
                    report: "TelerikReports.TelerikQuoteReport, TelerikReports",
                    parameters: { Id: '@ViewData["Id"]' }
                },
                viewMode: telerikReportViewer.ViewModes.INTERACTIVE,
                scaleMode: telerikReportViewer.ScaleModes.SPECIFIC,
                scale: 1.0,
                ready: function () {
                    //this.refreshReport();
                }
            });
        });

    So my question is in the case of using object datasource in reports, how could we dynamically change the datasource so we can use the same report template with the same "type" of data which is essentially replacing with different instances.

    Thank you.
  2. Nasko
    Admin
    Nasko avatar
    1042 posts

    Posted 22 Aug 2014 Link to this post

    Hello Rui,

    The recommended approach is to pass the ID to a report parameter:
    parameters: { Id: '@ViewData["Id"]' }

    and then in the report definition pass the parameter value to an ObjectDataSource component parameter.

    For more details on this approach, please refer to the Using Parameters with the ObjectDataSource Component help article.

    Regards,
    Nasko
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Rui
    Rui avatar
    4 posts
    Member since:
    Mar 2014

    Posted 22 Aug 2014 in reply to Nasko Link to this post

    Hi Nasko,

    Thank you for your reply. However,as you can see from my code in the initial post. I locked the parameter in view using:

    reportSource: {
                    report: "TelerikReports.TelerikQuoteReport, TelerikReports",
                    parameters: { Id: '@ViewData["Id"]' }
                },

    And on the server side, I want to access the Id parameter as followeed

    public TelerikQuoteReport()
            {
                InitializeComponent();
            }
     
            private void TelerikQuoteReport_NeedDataSource(object sender, EventArgs e)
            {
                Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender;
                   var test = report.Parameters["Id"].Value;
            }

    Result is I could not see the parameter Id, how could I pass parameter from view to server side this way?
  4. Rui
    Rui avatar
    4 posts
    Member since:
    Mar 2014

    Posted 22 Aug 2014 in reply to Rui Link to this post

    I solved the problem myself. I should manually create a report parameter named Id first during design time.
  5. sdf
    sdf avatar
    1 posts
    Member since:
    Feb 2016

    Posted 24 May 2016 Link to this post

    Hi Rui,

    Please provide code base or pic of above resolved issue.

    I have a view in which i need to generate report based on selected "id", i passed id as parameter from the view and need to get the value of id in report.cs and then need to pass it in select method of object data source. So i need a report that display record based on singly id.

    please find attached images with highlighted code.

    please resolve it.

    Thanks,

    Mohan

  6. Rui
    Rui avatar
    4 posts
    Member since:
    Mar 2014

    Posted 24 May 2016 in reply to sdf Link to this post

    Hi Mohan,

    The way that works for me is as followed.

    The part that supplies parameters is similar as in your screenshot. I used ViewData to provide dynamic data. You only need to supply parameters you want (I guess parameter case may matter, in your case would be "ID") and rely on the report framework to bind them to your data retrieving method. If you debug your host application and break at the data retrieving method you should see parameters coming through. Hope this can help you.

    01.// Report action to show report for a document
    02.public ActionResult Report(Guid docid)
    03.{
    04.   TempData["parameters"] = new Dictionary<String, String>() { { "id", doc.Id.ToString() }, { "user", AppContext.Identity.Id.ToString() } };
    05.   return RedirectToAction("Index", "ReportViewer", new
    06.   {
    07.      name = GetReportName(),
    08.      backurl = Url.Action("Id", GetViewFolderName(), new { Id = docid })
    09.   });
    10.}
    11. 
    12.// Unified hub action for rendering report
    13.public ActionResult Index(string name, string backurl)
    14.{
    15.   // Code omitted for simplicity
    16.   if (TempData.ContainsKey("parameters"))
    17.      ViewData["parameters"] = TempData["parameters"];
    18.   return View();
    19.}
    20. 
    21.// Report html viewer
    22.parameters: {
    23.            @if (ViewData["parameters"] != null)
    24.            {
    25.               foreach (KeyValuePair<string, string> param in ViewData["parameters"] as Dictionary<string, string>)
    26.               {
    27.                   @(param.Key)<text>: '</text>@(param.Value)<text>',</text>
    28.               }
    29.            }
    30.}
    31. 
    32.// Object datasource member method for retrieving data
    33.public TView GetByDisplayId(string id, string user)
    34.{
    35.   // Code omitted for simplicity
    36.   // Parameter id and user would be bound by Telerik report framework,
    37.   // You should be able to access them here and render the data you want
    38.}

  7. Chris
    Chris avatar
    100 posts
    Member since:
    Oct 2015

    Posted 16 Jan Link to this post

    HI

    I have a similar question.

    I need to get the real report parameter value in Report's constructor (not NeedDataSource event)
    for "Create Report Items Programmatically" purpose.

    https://docs.telerik.com/reporting/programmatic-creating-controls

        public TelerikQuoteReport()
        {
            InitializeComponent();

            ... get real parameter of report here ...

            ... Create Report Items Programmatically here ...

        }

    Pass values to report parameters via Telerik.Reporting.TypeReportSource : 

      .cshtml 

      @(Html.TelerikReporting().ReportViewer()
        ...
        .ReportSource(NewTypeReportSource)
        ...
        ...
        ...)

    Is there have any solution ? 

    Best regards

    Chris
  8. Chris
    Chris avatar
    100 posts
    Member since:
    Oct 2015

    Posted 16 Jan in reply to Chris Link to this post

    fix:

    ... get real parameter of report here ...

    ->

    ... get real value of parameter of report here ...
  9. Milen
    Admin
    Milen avatar
    178 posts

    Posted 18 Jan Link to this post

    Hi Chris,

    Try handling the ItemDataBinding event of the Report itself. You will have access to the report parameter evaluated values thru:

    //Take the Telerik.Reporting.Processing.Report instance
    Telerik.Reporting.Processing.Report report = (Telerik.Reporting.Processing.Report)sender;
     
    var x = report.Parameters["ManagerID"].Value;

    In this event handler, it is still safe to modify the report definition. Hope this helps.

    Regards,
    Milen
    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
  10. Chris
    Chris avatar
    100 posts
    Member since:
    Oct 2015

    Posted 22 Jan in reply to Milen Link to this post

    HI

    I know there have a way for modify the field of the report (ItemDataBinding event),
    but my request is get real value of parameter of report then
    "Create Report Items Programmatically".

    There maybe have no any soluton for that request (because of InstanceReportSource obsoleted).

    I will render the report to pdf on the fly and return the file bytes (FileContentResult).

    Best regards

    Chris

     

     

  11. Todor
    Admin
    Todor avatar
    225 posts

    Posted 24 Jan Link to this post

    Hello Chris,

    In the Report item ItemDataBinding event you may take the real processing values of the Report Parameters, e.g. as passed from the viewer. You may then modify/add report processing items (i.e. from Telerik.Reporting.Processing namespace) to the report, and this will be reflected in the generated reporting document. For details you may see the Changes on items in report events are not applied KB article.

    Indeed we do *not* recommend to use the InstanceReportSource. It is the only way to pass an instance of report to the Reporting engine, hence it can be used in case it is absolutely necessary to modify report definition run time.

    Regards,
    Todor
    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
Back to Top