Windows Forms: Create Report Viewer using Stored Procedure in C#
By FoxLearn 6/8/2017 7:41:53 PM 11.33K
How to Create a RDLC Report with Report Viewer using Entity Framework, Stored Procedure in C#
Step 1: Click 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
Step 2: Design your form as below
Main form: Form1
Print form: frmPrint
Step 3: Design your report as below
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
- How to Create QR Code in RDLC Report in C#
- How to Print RDLC Report without Report Viewer in C#
- How to Display an Image in Report Viewer using C#
- Windows Forms: Print Orders/Receipt using Report Viewer in C#
- Windows Forms: How to create a Chart / Graph using RDLC Report in C#
- Windows Forms: Print Receipt using Report Viewer in C#
Categories
Popular Posts
How to sign a powershell script
10/03/2024
How to get Credentials in PowerShell?
10/03/2024
How to implement Jint in C#
09/14/2024