Search results
Suggest a FeaturePDF

Render RDLC report

The data binding support allows you to view the RDLC reports that exist on the local file system with JSON array and custom business object data collection. The following steps demonstrates how to render a RDLC report with JSON array and custom business object data collection.

Add the RDLC report Product List.rdlc from Bold Reports installation location to your application wwwroot/Resources folder. For more information, see Samples and demos.

Bind data source in Web API controller

The following steps help you to configure the Web API to render the RDLC report with business object data collection.

  1. Create a class and methods that returns business object data collection. Use the following code in your application Web API Service.

    public class ProductList
    {
        public string ProductName { get; set; }
        public string OrderId { get; set; }
        public double Price { get; set; }
        public string Category { get; set; }
        public string Ingredients { get; set; }
        public string ProductImage { get; set; }
    
        public static IList GetData()
        {
            List<ProductList> datas = new List<ProductList>();
            ProductList data = null;
            data = new ProductList()
            {
                ProductName = "Baked Chicken and Cheese",
                OrderId = "323B60",
                Price = 55,
                Category = "Non-Veg",
                Ingredients = "grilled chicken, corn and olives.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Delite",
                OrderId = "323B61",
                Price = 100,
                Category = "Non-Veg",
                Ingredients = "cheese, chicken chunks, onions & pineapple chunks.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Tikka",
                OrderId = "323B62",
                Price = 64,
                Category = "Non-Veg",
                Ingredients = "onions, grilled chicken, chicken salami & tomatoes.",
                ProductImage = ""
            };
            datas.Add(data);
    
            return datas;
        }
    }
  2. Set the value of the ProcessingMode property to ProcessingMode.Local in the RDLC report location.

  3. To load a report as a stream, create a report stream using the FileStream class, and assign the report stream to the Stream property.

  4. Bind the business object data values collection by adding a new item to the DataSources as in the following code snippet.

        private IMemoryCache _cache;
        private IWebHostEnvironment _hostingEnvironment;
    
        public ReportViewerController(IMemoryCache memoryCache, IWebHostEnvironment hostingEnvironment)
        {
            _cache = memoryCache;
            _hostingEnvironment = hostingEnvironment;
        }
    
        [NonAction]
        public void OnInitReportOptions(ReportViewerOptions reportOption)
        {
            string basePath = _hostingEnvironment.WebRootPath;
            reportOption.ReportModel.ProcessingMode = ProcessingMode.Local;
            FileStream inputStream = new FileStream(basePath + @"\Resources\Product List.rdlc", FileMode.Open, FileAccess.Read);
            MemoryStream reportStream = new MemoryStream();
            inputStream.CopyTo(reportStream);
            reportStream.Position = 0;
            inputStream.Close();
            reportOption.ReportModel.Stream = reportStream;
            reportOption.ReportModel.DataSources.Add(new BoldReports.Web.ReportDataSource { Name = "list", Value = ProductList.GetData() });
        }

    Here, the Name is case sensitive and it should be same as in the data source name in the report definition. The Value accepts IList, DataSet, and DataTable inputs.

In the previous code, the Product List.rdlc report is loaded from the wwwroot/Resources folder location.

Bind data source with razor view

The following steps help you to provide the data source for a Report Viewer in razor view using the ViewBag.

  1. Create a class and methods that returns business object data collection. Use the following code in your application.

    public class ProductList
    {
        public string ProductName { get; set; }
        public string OrderId { get; set; }
        public double Price { get; set; }
        public string Category { get; set; }
        public string Ingredients { get; set; }
        public string ProductImage { get; set; }
    
        public static IList GetData()
        {
            List<ProductList> datas = new List<ProductList>();
            ProductList data = null;
            data = new ProductList()
            {
                ProductName = "Baked Chicken and Cheese",
                OrderId = "323B60",
                Price = 55,
                Category = "Non-Veg",
                Ingredients = "grilled chicken, corn and olives.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Delite",
                OrderId = "323B61",
                Price = 100,
                Category = "Non-Veg",
                Ingredients = "cheese, chicken chunks, onions & pineapple chunks.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Tikka",
                OrderId = "323B62",
                Price = 64,
                Category = "Non-Veg",
                Ingredients = "onions, grilled chicken, chicken salami & tomatoes.",
                ProductImage = ""
            };
            datas.Add(data);
    
            return datas;
        }
    }
  2. Create BoldReports.Models.ReportViewer.ReportDataSource collection with business object collection as follows and assign to the ViewBag to provide the data for report dataset.

            public IActionResult Index()
            {
                BoldReports.Models.ReportViewer.ReportDataSource reportDataSource = new BoldReports.    Models.ReportViewer.ReportDataSource();
                reportDataSource.Name = "list";
                reportDataSource.Value = ProductList.GetData();
                ViewBag.dataSources = new List<BoldReports.Models.ReportViewer.ReportDataSource> {     reportDataSource };
                return View();
            }
  3. Pass the ViewBag value to the dataSources property of Report Viewer.

        <bold-report-viewer id="viewer"
            report-service-url="/api/ReportViewer"
            dataSources="ViewBag.dataSources"
            processing-mode="Local" >
        </bold-report-viewer>

View report click

You can get the user selected parameter details when users click the ViewReport button in the parameter block. The view-report-click event allows you to handle the ViewReport button click at client-side as shown in the following code.

    <bold-report-viewer id="viewer" report-path="sales-order-detail.rdl" report-service-url="/api/ReportViewer"  view-report-click="onViewReportClick" processing-mode="Remote"></bold-report-viewer>
    <script type="text/javascript">
        function onViewReportClick(args) {
            args[0] = ({ name: 'SalesOrderNumber', labels: ['SO50756'], values: ['SO50756'], nullable: false });
        }
    </script>

The model property in the event argument has the details of current processing report model.