Category / Section
How to assign the data from asynchronous (async) service in Syncfusion ASP.NET Core reportviewer?
2 mins read
You can assign the data from asynchronous (async) service using Syncfusion ASP.NET Core ReportViewer. The following steps guide you to assign the data from asynchronous (async) service from the OnReportLoaded method in the Web API controller.
1. Create a simple ReportViewer application with the help of getting started documentation.
2. Get asynchronous (async) service data in the Web API controller from the OnReportLoaded method as shown in the following code snippet.
HomeController.cs
public partial class HomeController : Controller, IReportController { private IMemoryCache _cache; private IHostingEnvironment _hostingEnvironment; public HomeController(IMemoryCache memoryCache, IHostingEnvironment hostingEnvironment) { _cache = memoryCache; _hostingEnvironment = hostingEnvironment; } public ActionResult Index() { return View(); } public IActionResult Error() { return View(); } [HttpPost] public object PostReportAction([FromBody] Dictionary<string, object> jsonResult) { if (jsonResult.ContainsKey("CustomData")) { var paramValue = jsonResult["CustomData"]; } return ReportHelper.ProcessReport(jsonResult, this, this._cache); } [ActionName("GetResource")] [AcceptVerbs("GET")] public object GetResource(ReportResource resource) { return ReportHelper.GetResource(resource, this, _cache); } [HttpPost] public object PostFormReportAction() { return ReportHelper.ProcessReport(null, this, this._cache); } public void OnInitReportOptions(ReportViewerOptions reportOption) { string basePath = _hostingEnvironment.WebRootPath; FileStream inputStream = new FileStream(basePath + @"\ReportData\Region.rdlc", FileMode.Open, FileAccess.Read); reportOption.ReportModel.Stream = inputStream; } public async void OnReportLoaded(ReportViewerOptions reportOption) { if (reportOption.ReportModel.ProcessingMode == ProcessingMode.Local) { reportOption.ReportModel.DataSources.Clear(); var ReportData = await GetData(); reportOption.ReportModel.DataSources.Add(new ReportDataSource { Name = "StoreSales", Value = ReportData }); } } private async Task<List<StoreSales>> GetData() { List<StoreSales> sales = new List<StoreSales>(); StoreSales strSal = null; strSal = new StoreSales() { SalesOrderID = 43659, TotalDue = 23153.2339, CustomerID = 29825, StateProvinceCode = "GA", Store = "Better Bike Shop" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43663, TotalDue = 472.3108, CustomerID = 29565, StateProvinceCode = "CA", Store = "World Bike Discount Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43673, TotalDue = 4216.0258, CustomerID = 29844, StateProvinceCode = "NH", Store = "Seventh Bike Store" }; sales.Add(strSal); strSal = new StoreSales() { SalesOrderID = 43675, TotalDue = 6434.0848, CustomerID = 29827, StateProvinceCode = "MO", Store = "First Bike Store" }; sales.Add(strSal); return await Task.Run(() => sales); } } public class StoreSales { public int SalesOrderID { get; set; } public double TotalDue { get; set; } public int CustomerID { get; set; } public string Store { get; set; } public string StateProvinceCode { get; set; } }
Please download the sample for asynchronous data here