Report parameter in report constructor

6 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
    909 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. DevCraft banner
  4. 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?
  5. 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.
  6. sdf
    sdf avatar
    1 posts
    Member since:
    Feb 2016

    Posted 24 May 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

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

    Posted 24 May 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.}

Back to Top
DevCraft banner