How i can create report viewer get data from database in Asp Net MVC?
Loading
How i can create report viewer get data from database in Asp Net MVC?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pasang TamangRank #449 · 5.6K reputationPosted Mar 28, 2023, 11:35 AM EDT
Hi,
Please refer to this link https://www.c-sharpcorner.com/article/display-data-in-report-viewer-with-mvc-4/. I hope this helps to you.
Regards
Pasang
Vishal YelveRank #178 · 13.6K reputationPosted Mar 28, 2023, 12:51 PM EDT
Hi,
Refer below links
https://www.aspsnippets.com/Articles/Implementing-RDLC-Reports-in-ASPNet-MVC.aspx
https://www.boldreports.com/blog/how-to-add-the-report-viewer-to-an-aspnet-mvc-application
https://www.c-sharpcorner.com/article/display-data-in-report-viewer-with-mvc-4/
Amit MohantyRank #112 · 20K reputationPosted Mar 29, 2023, 12:22 AM EDT
First install the below NuGet packages to your project:
Create a new ASP.NET MVC Controller that will handle the report viewer request. In this controller, you will write the code to retrieve data from the database and populate the report.
Create a new Report Definition (.rdlc) file in your ASP.NET MVC project. In this file, you will define the layout and structure of the report.
Add a new View to your ASP.NET MVC project that will display the report viewer control. In this view, you will add the report viewer control and configure it to load the report definition file and retrieve data from the controller.
Here is an example. Controller code:
public class ReportController : Controller { public ActionResult Index() { var reportData = GetDataFromDatabase(); ReportViewerModel reportModel = new ReportViewerModel(); reportModel.ReportPath = Server.MapPath("~/Reports/MyReport.rdlc"); reportModel.ReportDataSources.Add(new ReportDataSource("MyReportDataSet", reportData)); return View(reportModel); } private List GetDataFromDatabase() { // code to retrieve data from database using ADO.NET or Entity Framework } }View code:
@model Microsoft.Reporting.WebForms.ReportViewerModel @{ ViewBag.Title = "My Report Viewer"; } @ViewBag.Title @Html.ReportViewer(Model)Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 29, 2023, 4:33 PM EDT
To create a report viewer and get data from the database in an ASP.NET MVC application, you can follow these steps:
Create a new
ReportViewercontrol in your view page:In your controller, create a method that retrieves data from the database and returns it in a
DataTable:public DataTable GetData() { string connectionString = "your_connection_string_here"; SqlConnection connection = new SqlConnection(connectionString); string query = "SELECT * FROM your_table_here"; SqlCommand command = new SqlCommand(query, connection); SqlDataAdapter dataAdapter = new SqlDataAdapter(command); DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); return dataTable; }In your view page, add the following code to bind the ReportViewer control with the data:reportViewer1.ProcessingMode = ProcessingMode.Local; reportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/your_report_name_here.rdlc"); ReportDataSource reportDataSource = new ReportDataSource("DataSet1", GetData()); reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.DataSources.Add(reportDataSource); reportViewer1.LocalReport.Refresh();Make sure to replace
your_connection_string_here,your_table_here,your_report_name_here, andDataSet1with the appropriate values for your application.Also, make sure that the report file exists in the
Reportsfolder in your application and that its name and path are correct in the code.