How to Create Report Viewer using Stored Procedure in C#

By FoxLearn 11/14/2024 12:30:46 PM   11.47K
Creating an RDLC report with the Report Viewer using Entity Framework and a stored procedure in C# involves several steps.

Report Viewers are UI components that allow you to display a report document produced by a report engine within the application's user interface. They act as a container to render, view, and interact with reports.

In C#, the ReportViewer control provides functionality for displaying the report, navigating through pages, and offering additional features like print, export, and zoom options.

Open Visual Studio and create a Windows Forms Application project by clicking New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "PrintProducts" and then click OK

How to Create Report Viewer using Stored Procedure in C#

Drag the DataGridView, Button controls from Visual Toolbox onto your form designer, then design your form as below.

Main form: Form1

rdlc c#

You need create a stored procedure to retrieve product data from the Northwind database

For example:

create procedure [dbo].[GetProductList]
as
	select ProductID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock
	from Products

This stored procedure will return the Product details that we will display in the RDLC report.

If you haven't got Northwind database, you can view How to download and restore Northwind database to Sql Server

Next, Create an Entity Framework Model First by right-clicking on your project → AddNew ItemADO.NET Entity Data Model.

Choose EF Designer from Database and follow the prompts to connect to your database, then add the GetProductList to your EF Model.

Add code to handle your Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Objects;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrintProducts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //Get data from data source, then open print form dialog
            List<GetProductList_Result> list = getProductListResultBindingSource.DataSource as List<GetProductList_Result>;
            if (list != null)
            {
                using (frmPrint frm = new frmPrint(list))
                {
                    frm.ShowDialog();
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Load data to data source
            using(NorthwindEntities db = new NorthwindEntities())
            {
                getProductListResultBindingSource.DataSource = db.GetProductList().ToList();
            }
        }
    }
}

In your Form1 code-behind, you will call the stored procedure through Entity Framework and bind the result to the DataGridView.

Next, Right-click on your project and select AddNew ItemReport

Choose Report (RDLC) and enter your report name. This will open the RDLC Report Designer in Visual Studio.

In the RDLC designer, Right-click on the Report Data pane → Add DataSourceObject.

Choose the object type as the class you expect the stored procedure to return.

Drag and drop the fields from the DataSource onto the report, then design your report as below.

c# rdlc report

Next, Create a new form called frmPrint

c# print rdlc

Open frmPrint form, then drag and drop the ReportViewer control from the toolbox onto the form.

The Report Viewer controls can be used to integrate Reporting Services Report Definition Language (RDL) reports into WebForms and WinForms applications. These controls enable the embedding of RDL-based reports within the application UI, allowing users to view, interact with, and manipulate reports (such as pagination, export, and printing) directly within the app. The Report Viewer can be used with both local reports (RDLC files) and server-based reports (RDL) hosted on SQL Server Reporting Services (SSRS).

    Add code to handle frmPrint

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace PrintProducts
    {
        public partial class frmPrint : Form
        {
            List<GetProductList_Result> list;
    
            //Passing parameters to constructor
            public frmPrint(List<GetProductList_Result> list)
            {
                InitializeComponent();
                this.list = list;
            }
    
            private void frmPrint_Load(object sender, EventArgs e)
            {
                //Get data from constructor
                GetProductList_ResultBindingSource.DataSource = list;
                this.reportViewer1.RefreshReport();
            }
        }
    }

    For using RDLC reports, ensure you have the necessary Report Viewer package installed:

    Right-click on the project in Solution Explorer → Manage NuGet Packages → Install Microsoft.ReportViewer.WinForms

    Now, when you run the application, the ReportViewer control will display the RDLC report, populated with the data from your stored procedure.

    VIDEO TUTORIAL