Changing a parameter's datasource connection string at runtime

8 posts, 0 answers
  1. James
    James avatar
    2 posts
    Member since:
    Sep 2014

    Posted 14 Oct 2014 Link to this post

    I created a report in the report designer.
    I have a parameter for a report that connects to a datasource.
    It pulls back a list of items from which the user can select one to run the report against.
    That parameter pulls its data from a datasource that I added via the report designer.
    I added the resulting trdx file to my winForms visual studio project.
    I need to be able to change the connection string of the datasource the parameter is using at run time.
    I was able to deserialize the trdx file easily enough with some code I found here in this forum.
    But I could not figure out how to change the connectionstring for a parameter's datasource.
    How would I go about doing that?

    Thanks,Jim
  2. Stef
    Admin
    Stef avatar
    3036 posts

    Posted 16 Oct 2014 Link to this post

    Hello Jim,

    Please test usingthe approach illustrated in the code snippet of the Changing the connection string dynamically according to runtime data KB article.

    Let us know how it goes and if you need further help.

    Regards,
    Stef
    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. Roberto
    Roberto avatar
    2 posts
    Member since:
    Feb 2013

    Posted 09 Jan 2015 in reply to Stef Link to this post

    I am trying to change the connection string of a TRDX Report at runtime in an MVC 4 application.  I am using this page as a guide:

    http://www.telerik.com/support/kb/reporting/details/changing-the-connection-string-dynamically-according-to-runtime-data

    However I end up getting this error: "Unable to get report parameters: Missing report name"

    Here is the ReportsServiceController:
    public class ReportsServiceController : ReportsControllerBase
       {
           protected override IReportResolver CreateReportResolver()
           {
               var reportsPath = HttpContext.Current.Server.MapPath("~/Content/Reports");
     
               return new ReportFileResolver(reportsPath)
                   .AddFallbackResolver(new ReportTypeResolver());
           }
     
           protected override ICache CreateCache()
           {
               return Telerik.Reporting.Services.Engine.CacheFactory.CreateFileCache();
           }
     
       }

    Code for the controller for the view containing the ReportViewer

    public ActionResult Index()
           {
               var connStringManager = new ReportConnectionStringController();
               var uri = Server.MapPath("/Content/Reports/Venta.trdx");
               var report = new UriReportSource() { Uri = uri };
               report.Parameters.Add(new Parameter("ptIdUnica", 3));
               var changeReport = connStringManager.UpdateReportSource(report) as InstanceReportSource;
               return View(new ReportModel() { TheReport = changeReport });
           }

    that ReportModel.TheReport
    public class ReportModel
        {
            public InstanceReportSource TheReport { get; set; }
        }

    And the View:
    @model PuntoVentaWeb.ViewModels.ReportModel
    @using Telerik.ReportViewer.Mvc
    @section styles
    {
        <link href="~/ReportViewer/styles/ReportViewer-7.2.14.127.css" rel="stylesheet" />
     
        <style>
            #reportViewer1 {
                position: absolute;
                left: 5px;
                right: 5px;
                top: 5px;
                bottom: 5px;
                overflow: hidden;
            }
        </style>
    }
     
    @section scripts
    {
     
        <!--kendo.all.min.js can be used as well instead of kendo.web.min.js and kendo.mobile.min.js-->
        <!--kendo.mobile.min.js - optional, if gestures/touch support is required-->
     
        <link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
        <script src="~/ReportViewer/js/ReportViewer-7.2.14.127.js"></script>
    }
    @{
        ViewBag.Title = "Report Viewer";
         
        @(Html.TelerikReporting().ReportViewer()
        .Id("reportViewer1")
        .ServiceUrl("/api/ReportsService/")
        .TemplateUrl(@Url.Content("~/ReportViewer/templates/telerikReportViewerTemplate.html"))
        .ReportSource(Model.TheReport)
        .ViewMode(ViewModes.INTERACTIVE)
        .ScaleMode(ScaleModes.SPECIFIC)
        .Scale(1.0)
        .PersistSession(false)
        )
    }

    Please Any help is appreciated

    Stef said:Hello Jim,

    Please test usingthe approach illustrated in the code snippet of the Changing the connection string dynamically according to runtime data KB article.

    Let us know how it goes and if you need further help.

    Regards,
    Stef
    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.

     

  5. Stef
    Admin
    Stef avatar
    3036 posts

    Posted 12 Jan 2015 Link to this post

    Hello Roberto,

    Please check the details regarding the HTML5 Report Viewer in the Call service to bind the report forum thread (How the viewer(client)-service pair works).


    In order to change the connection strings in the report, you will need a custom report resolver for the Reporting REST service's CreateReportResolver method.

    In that resolver's Resolve method you can place the logic for replacing the connection strings and returning a valid report source object.


    Let us know if you have any further questions.

    Regards,
    Stef
    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.

     
  6. Craig
    Craig avatar
    16 posts
    Member since:
    Jun 2014

    Posted 17 Sep 2015 Link to this post

    I am also trying to set the connection string dynamically using http://www.telerik.com/support/kb/reporting/details/changing-the-connection-string-dynamically-according-to-runtime-data. I have not been able to find any of the posts helpful.

     I use an MVC View and am trying to use what was mentioned in the above post but have not been able to successfully get it to work. I am also passing parameters into the UriReportSource.

      var reportSource = new Telerik.Reporting.UriReportSource()
            {
                Uri = "MyReport.trdx",
                Parameters = {new Parameter { Name="​x" , Value = Model.X}, 
                new Parameter {Name = "​y", Value= ​Model.Y},
                new Parameter {Name = "​z", Value=Model.​Z},             
                }
            };
            var connectionString = ​​"[insert connection string here]";
            var connectionStringHandler = new ​ProjectName.Classes.ReportConnectionStringManager(connectionString);
            var newReportSource = connectionStringHandler.UpdateReportSource(reportSource);
                
        @(Html.TelerikReporting().ReportViewer()
                .Id("reportViewer1")
                .ServiceUrl(ConfigurationManager.AppSettings["basePath"] + "/api/ReportSetup/")
                .TemplateUrl(ConfigurationManager.AppSettings["basePath"] +  "/ReportViewer/templates/telerikReportViewerTemplate-8.1.14.618.html") 
                .ReportSource(newReportSource)
                
                )

    Instead of pointing me to a custom report resolver, if that is what is needed, please provide me exactly how I can do it in code and where to place the code. An example project would be helpful too. Help would be greatly appreciated. Thanks!

  7. Stef
    Admin
    Stef avatar
    3036 posts

    Posted 22 Sep 2015 Link to this post

    Hello Craig,

    The HTML5 Viewer  sends only string description which report to be resolved on the server, not the whole report instance - How it works.
    Thus you can keep the current settings where you pass a server path to a TRDX file. Then you will have to use a custom resolver in the Reporting REST service to get the passed string, obtain the TRDX file, deserialize and modify it.


    For example:
    static ReportsController()
          {
             // var appPath = HttpContext.Current.Server.MapPath("~/");
            //  var reportsPath = Path.Combine(appPath, @"..\..\..\Report Designer\Examples");
             // var resolver = new ReportFileResolver(reportsPath)
              //    .AddFallbackResolver(new ReportTypeResolver());
        
              configurationInstance = new ReportServiceConfiguration
              {
                  HostAppId = "Html5DemoApp",
                  Storage = new FileStorage(),
                  ReportResolver = new MyResolver(),//resolver,
                  //ReportSharingTimeout = 0,
                  //ClientSessionTimeout = 0,
              };
          }
            
    //the custom resolver
        public class MyResolver : IReportResolver
        {
            //this method will be called on each request for report (refresh, navigate to report, sub report, parameters updates)
           //the method will be called 3 times on initial load
           public Telerik.Reporting.ReportSource Resolve(string report)
            {         
                Report reportInstance =null;
               if(report.Contains("SampleReport"))
               {
               //retrieve an instance of the report      
                reportInstance = DeserializeReport(report);;
          
               //change the report's DataSource settings
                (reportInstance.DataSource as Telerik.Reporting.SqlDataSource).ConnectionString = "new string here";
           
               //change a nested data item's DataSource
                ((reportInstance.Items.Find("table1",true)[0] as Telerik.Reporting.Table).DataSource =GetStoredOnTheServerData();
                //set report parameters values
                  reportInstance.ReportParameters["UserId"].Value = GetUserId();
               }
                return new InstanceReportSource { ReportDocument = reportInstance};
            }
        }
    /////////////////test the above with calling a randome SampleReport report
    <script type="text/javascript">
            $(document).ready(function () {
                $("#reportViewer1")
                    .telerik_ReportViewer({       
                        serviceUrl: "api/reports/",
                        templateUrl: 'ReportViewer/templates/telerikReportViewerTemplate-9.1.15.731.html',
                        //ReportSource - report description
                        reportSource: {
                            report: "SampleReport.trdx",
                            // Parameters name value dictionary
                            parameters: { }
                        },
                        viewMode: telerikReportViewer.ViewModes.INTERACTIVE,
                    });
            });
    In the custom resolver's Resolve method you can include the logic from the Changing the connection string dynamically according to runtime data KB article.


    I hope this information is helpful.

    Regards,
    Stef
    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
  8. Al
    Al avatar
    203 posts
    Member since:
    Oct 2006

    Posted 08 Jun in reply to Stef Link to this post

    Hi,
    When implementing your code above, the line:
    reportInstance = DeserializeReport(report);;
    Gives an error 'The name 'DeserializeReport' does not exist in the current context'. AS far as I can tell I have all the correct references/usings - please advise?
  9. Al
    Al avatar
    203 posts
    Member since:
    Oct 2006

    Posted 08 Jun in reply to Al Link to this post

    Never mind - I see now that DeserializeReport comes from the ReportConnectionStringManager class so this code needs some reworking.
Back to Top
DevCraft banner