WebDesigner\JSViewer set DataSource and Datasets via code

Posted by: kevin.gunn on 2 September 2019, 6:42 am EST

    • Post Options:
    • Link

    Posted 2 September 2019, 6:42 am EST

    We store our page report layouts in a database.

    I have successfully implemented the retrieving, editing and saving of Page reports using a CustomStore.

    Can anyone advise how I can inject the datasource when calling using preview.

    Our queries are done using Linq as we use EF in our application, of which return DTO’s. I have successfully set the dataset using reflection on the DTO, and then creating the fields based on the properties.

    I would like to provide the end user, a dummy dataset so that they can preview a report, to ensure that it looks correctly.

    However I am struggling to achieve this, I have tried setting the type as JSON, and using NewtonJoft serializing a dummy DTO, setting that as the connection string and by serting the Query CommandText as $.Data[*] .

    Thanks

  • Posted 2 September 2019, 5:58 pm EST

    Hello Kevin,

    You can pass the DTO directly to the report using the “ObjectProvider”. To know more about the ObjectProvider, Please refer to the “Object Provider” topic in the following documentation link:

    https://help.grapecity.com/activereports/webhelp/AR13/webframe.html#BindPageReportstoaDataSource.html

    Please refer to the attached sample to implement the same with the JSViewer.

    Hope it helps.

    Thanks.

    JSViewer_MVC.zip

  • Posted 2 September 2019, 9:19 pm EST

    Hi Mohit,

    Thanks for the response, I have attempted to implement the viewer as per the example.

    However it is not hitting my method as defined below:

     app.UseReporting(settings =>
          {
            settings.UseCustomStore(ReportPreviewHelper.GetReport);
            settings.UseCompression = true;
            settings.LocateDataSource = args =>
            {
              var fakeData = new Faker<SMPIDTO>()
               .RuleFor(d => d.MemberName, f => f.Name.FullName())
               .RuleFor(d => d.BrandName, f => f.Lorem.Words(3).ToString());
              return fakeData.GenerateLazy(10);
            };
          });
    

    Do I still need to set the reportservice url for the viewer?

    Thanks

    Kev

  • Posted 2 September 2019, 9:25 pm EST - Updated 30 September 2022, 8:31 am EST

    Hello Kevin,

    Could you please check datasource in the report set to the “ObjectProvider” as shown in the report. Also, your dataset should contain the only fields without any query text.

    Please refer to the attached images.

    Do I still need to set the reportservice url for the viewer?

    No, if your startup.cs and JsViewer are in same project then there is no need to set ReportService url.

    Thanks.

  • Posted 2 September 2019, 9:34 pm EST

    Hi Mohit,

    How is this achieved in the Web Designer?

    Thanks

    Kev

  • Posted 2 September 2019, 10:03 pm EST

    Please ignore, I have it working now.

    Many Thanks for your help.

  • Posted 2 September 2019, 10:19 pm EST

    Hello,

    I am glad that it is worked at your end. However, It is not possible to set the “ObjectProvider” through Web designer. You can manually add the “ObjectProvider” in your report using the following code:

    
    private object GetReport(string arg)
            {
                PageReport rpt = new PageReport();
                rpt.Load(new System.IO.FileInfo(@"\test.rdlx"));
                GrapeCity.ActiveReports.PageReportModel.DataSource myDataSource = new GrapeCity.ActiveReports.PageReportModel.DataSource();
                myDataSource.Name = "Example Data Source";
                myDataSource.ConnectionProperties.DataProvider = "OBJECT";
                GrapeCity.ActiveReports.PageReportModel.DataSet myDataSet = new GrapeCity.ActiveReports.PageReportModel.DataSet();
                GrapeCity.ActiveReports.PageReportModel.Query myQuery = new GrapeCity.ActiveReports.PageReportModel.Query();
                myDataSet.Name = "DataSet1";
                myQuery.DataSourceName = "Example Data Source";
                myDataSet.Query = myQuery;
    
                GrapeCity.ActiveReports.PageReportModel.Field _field = new
    GrapeCity.ActiveReports.PageReportModel.Field("OrderID", "OrderID", null);
                myDataSet.Fields.Add(_field);
                _field = new GrapeCity.ActiveReports.PageReportModel.Field("CustomerID", "CustomerID", null);
                myDataSet.Fields.Add(_field);
                _field = new GrapeCity.ActiveReports.PageReportModel.Field("EmployeeID", "EmployeeID", null);
                myDataSet.Fields.Add(_field);
                //bind the data source and the dataset to the report
                rpt.Report.DataSources.Add(myDataSource);
                rpt.Report.DataSets.Add(myDataSet);
                return rpt.Report;
            }
    
    
    

    Hope it helps.

    Thanks.

  • Posted 3 September 2019, 5:58 pm EST

    Hi Mohit,

    Is there anything special I will need to do, to be able to get SubReports working in the WebDesigner, using DTO’s and ObjectProvider.

    I have currently tried just adding a Subreport and pointing it to a report as per this blog post https://www.grapecity.com/blogs/how-to-use-subreports-in-page-reports.

    However upon clicking Preview I I just get the following error, which I must say isn’t very helpful in diagnosing the problem! ```

    Rendering error:One or more errors occurred.

    
    Thanks
    
    Kev
  • Posted 3 September 2019, 9:37 pm EST - Updated 30 September 2022, 8:31 am EST

    Hello Kevin,

    You need to handle the LocateDataSource in “app. UseReporting” method in web Designer sample like below:

    app.UseReporting(config => { config.UseFileStore(ResourcesRootDirectory); config.LocateDataSource = args =>
                {
                    if (args.Report.Name == "test.rdlx") //SubReport Name
                    {
                        NorthwindEntities ts = new NorthwindEntities();
                        return ts.Orders.ToList();
                    }
                    else
                        return null;
                };
                });
    

    Hope it helps.

    Thanks.

  • Posted 9 September 2019, 6:01 am EST

    Hi Mohit,

    It doesn’t actually get to LocateDatasource.

    How does the viewer get the report contents of the sub report, we store all of our reports in a database.

    I do not see a 2nd call to GetReport.

    Thanks

    Kev

  • Posted 9 September 2019, 8:28 pm EST

    Hello,

    Ideally, it can be achieved through ResourceLocator class of the ActiveReports. However, I am facing the error while implementing the same. Hence, I have escalated this to our development team(275583) and will inform you once I get any information from them.

    Thanks,

    Mohit

  • Posted 9 September 2019, 10:41 pm EST

    Thanks Mohit.

  • Posted 17 September 2019, 3:11 pm EST

    Hello Kevin,

    I got a reply from the developer team.

    Please refer the following lines of code to load the report from the stream:

     public void Configuration(IAppBuilder app)
            {
             //   app.UseErrorPage();
    
                app.UseReporting(settings =>
                {
                    settings.UseCustomStore(GetReport);
                    settings.UseCompression = true;
                 
                    settings.LocateDataSource = args =>
                    {
                        DataTable dt= GetTable();
                     
                        return dt;
                    };
                });
    
          //      RouteTable.Routes.RouteExistingFiles = true;
            }
    
            private object GetReport(string arg)
            {
             
                var stream = new FileStream($@"C:\Users\MohitG\source\repos\JSViewer_MVC\JSViewer_MVC\main.rdlx", FileMode.Open);
                var resourceLocator = new SubReportResourceLocator();
    
                if (stream == null)
                    throw new ReportNotFoundException();
    
                using (var reader = new StreamReader(stream))
                {
                    var report = PersistenceFilter.Load(reader, resourceLocator);
                    report.Site = new ReportSite(resourceLocator);
                    if (string.IsNullOrEmpty(report.Name))
                        report.Name = "Main.rdlx";
                    return report;
                }
            }
    
       public class ReportSite : ISite
        {
            private readonly ResourceLocator _resourceLocator;
    
            public ReportSite(ResourceLocator resourceLocator)
            {
                _resourceLocator = resourceLocator;
            }
    
            public object GetService(Type serviceType) =>
                serviceType == typeof(ResourceLocator) ? _resourceLocator : null;
    
            public IComponent Component => null;
            public IContainer Container => null;
            public bool DesignMode => false;
            public string Name { get; set; }
        }
        public class SubReportResourceLocator : ResourceLocator
        {
            public override Resource GetResource(ResourceInfo resourceInfo)
            {
                Resource _resource;
                Uri uri = new Uri("file:///C:/Users/MohitG/source/repos/JSViewer_MVC/JSViewer_MVC/LocateDataSoource.rdlx");
    
                if (resourceInfo.Name.Equals("LocateDataSoource.rdlx"))
                {
                    byte[] _buffer = System.Text.UTF8Encoding.UTF8.GetBytes(File.ReadAllText(@"C:\Users\MohitG\source\repos\JSViewer_MVC\JSViewer_MVC\LocateDataSoource.rdlx"));
                    MemoryStream _ms = new MemoryStream(_buffer);
                    if (_ms != null)
                    {
                        _resource = new Resource(_ms,uri);
                    }
                }
                return _resource;
            }
        }
    

    Hope it helps.

    Thanks,

    Mohit

  • Posted 17 September 2019, 4:12 pm EST

    Great Thanks Mohit. i will give this a try once I’ve sorted the other bits.

    Kev

  • Posted 22 September 2019, 4:56 pm EST

    Hello Kevin,

    I have received the notification that you asked the following question on this thread:

    Hi Mohit,

    The above code wont work for us, we store our reports in a database. I’m

    struggling to work out what to put as the URI.

    Thanks

    Kev"


    If you are still facing the same issue, please set the URL to your server URL. It will solve your issues.

    Thanks,

    Mohit

  • Posted 22 September 2019, 10:24 pm EST

    Hi Mohit,

    Sorry I have managed to get this working.

    Thanks

    Kev

  • Posted 22 September 2019, 10:35 pm EST

    Hello Kevin,

    I am glad that you solved the issue at your end. I am assuming that you are now able to integerate Web Designer in your application without any issue.

    Thanks,

    Mohit

  • Posted 23 September 2019, 11:27 pm EST

    Hi Mohit,

    Yes I have successfully integrated the web designer into our Application, thanks for all your help.

    Kev

  • Posted 23 September 2019, 11:35 pm EST

    Great :slight_smile: Please feel free to ask if you face any issue further.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels