Windows Forms: Create Report Viewer using Stored Procedure in C#

By FoxLearn 6/8/2017 7:41:53 PM   11.07K
How to Create a RDLC Report with Report Viewer using Entity Framework, Stored Procedure in C#

Step 1Click 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

print rdlc c#Step 2: Design your form as below

Main form: Form1

rdlc c#

Print form: frmPrint

c# rdlc

Step 3: Design your report as below

c# rdlc report

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

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

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

Step 5: Create an Entity Framework Model First, then add the GetProductList to your EF Model

Add code to handle 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();
            }
        }
    }
}

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();
        }
    }
}

VIDEO TUTORIALS