How to create, print barcodes in RDLC files in C# Windows applications?
Prerequisites
- Visual Studio 2022 (with Microsoft RDLC Report Designer)
- .NET Framework 4.7.2 or higher (supporting .NET Standard 2.0)
- (For Tutorial 1 only) Restore Microsoft sample database "AdventureWorksLT2019" to SQL Server Express LocalDB
Create new Windows Forms project
Start Visual Studio 2022 and select "Create a new project".
Select "Windows Forms App (.NET Framework)" in the dialog and then press "Next" button.
Create a new project with name "OnBarcodeRDLCWinFormsDemo".
Now, all auto-generated files could be found in the solution explorer.
Select "Windows Forms App (.NET Framework)" in the dialog and then press "Next" button.
Create a new project with name "OnBarcodeRDLCWinFormsDemo".
Now, all auto-generated files could be found in the solution explorer.
Add library and NuGet packages
Right-click "References" in the Solution Explorer, and select "Manage NuGet Packages".
Select "Browse" and use the search control to find "Microsoft.ReportingServices.ReportViewerControl.Winforms" from the package source "nuget.org".
Choose the latest stable version to install the package.
Install "OnBarcode.RDLC" version 10.0.1 or later.
Check "References" in the Solution Explorer to ensure the installation is success.
Select "Browse" and use the search control to find "Microsoft.ReportingServices.ReportViewerControl.Winforms" from the package source "nuget.org".
Choose the latest stable version to install the package.
Install "OnBarcode.RDLC" version 10.0.1 or later.
Check "References" in the Solution Explorer to ensure the installation is success.
Tutorial 1: How to create barcodes from database in RDLC report using C#?
Add data source
Add a new item "DataSet1.xsd" to the project.
Insert a TableAdapter from Toolbox to "DataSet1.xsd" and configure it with TableAdapter Configuration Wizard.
Press "New Connection" to add a connection.
Set Server name to (LocalDB)\MSSQLLocalDB and select the restored database AdventureWorksLT2019. (Try "Test Connection" to ensure the connection is successful.) Then, press
Note:
Backup file of the Microsoft sample database AdventureWorks Lightweight (2019) can be downloaded from below website. https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver17&tabs=ssms
Check the checkbox to verify the connection string and press
Save the connection as AdventureWorksLT2019ConnectionString.
Use "Query Builder" to specify a SQL statement to access the database.
Add table "Product (SalesLT)" in the database and select two fields "ProductID" and "Name"; and then, press
Verify the SQL statement and Finish the wizard.
Add a new column to the data table "Product" with column name "Barcode" and set its property Data Type to
Finally, the data table "Product" contains three columns as below.
Insert a TableAdapter from Toolbox to "DataSet1.xsd" and configure it with TableAdapter Configuration Wizard.
Press "New Connection" to add a connection.
Set Server name to (LocalDB)\MSSQLLocalDB and select the restored database AdventureWorksLT2019. (Try "Test Connection" to ensure the connection is successful.) Then, press
OK to create the connection string.
Note:
Backup file of the Microsoft sample database AdventureWorks Lightweight (2019) can be downloaded from below website. https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver17&tabs=ssms
Check the checkbox to verify the connection string and press
Next button to continue.
Save the connection as AdventureWorksLT2019ConnectionString.
Use "Query Builder" to specify a SQL statement to access the database.
Add table "Product (SalesLT)" in the database and select two fields "ProductID" and "Name"; and then, press
OK to finish.
Verify the SQL statement and Finish the wizard.
Add a new column to the data table "Product" with column name "Barcode" and set its property Data Type to
System.Byte[].
Finally, the data table "Product" contains three columns as below.
Design RDLC report template
Add a new Report item "Report1.rdlc" to the project.
And change property "Copy to Output Directory" to
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "Product" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ProductID", "Name" and "Barcode" to this Table in the report.
Insert an "Image" to Barcode field, and set "Image Properties" as below.
Finally, adjust table row height and column width to the appropriate size.
And change property "Copy to Output Directory" to
Copy if newer.
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "Product" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ProductID", "Name" and "Barcode" to this Table in the report.
Insert an "Image" to Barcode field, and set "Image Properties" as below.
Finally, adjust table row height and column width to the appropriate size.
Add control and behind C# code to the Form
Double click "Form1.cs" to open the designer and add "ReportViewer" control to the form.
Choose report OnBarcodeRDLCWinFormgsDemo.Report1.rdlc for this viewer control.
Add a ProductTableAdapter control with default name "productTableAdapter1" to the form.
Open "Choose Data Sources" dialog and select "Product" table in "DataSet1".
Then, it would create the DataSet "dataSet1" and a BindingSource "productBindingSource" on the form.
Select "View Code" from the menu to open the Form1.cs window.
Replace ALL contents in the Form1.cs file by following codes.
Choose report OnBarcodeRDLCWinFormgsDemo.Report1.rdlc for this viewer control.
Add a ProductTableAdapter control with default name "productTableAdapter1" to the form.
Open "Choose Data Sources" dialog and select "Product" table in "DataSet1".
Then, it would create the DataSet "dataSet1" and a BindingSource "productBindingSource" on the form.
Select "View Code" from the menu to open the Form1.cs window.
Replace ALL contents in the Form1.cs file by following codes.
Form1.cs
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; using OnBarcode.Barcode.RDLC; namespace OnBarcodeRDLCWinFormsDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.productTableAdapter1.Fill(this.dataSet1.Product); Linear linear = new Linear(); linear.Type = BarcodeType.CODE128; linear.BarcodeWidth = 500; linear.BarcodeHeight = 100; foreach (DataSet1.ProductRow row in this.dataSet1.Product.Rows) { linear.Data = row.ProductID.ToString(); linear.OutputFileFormat = FileFormat.PNG; row.Barcode = linear.drawBarcodeAsBytes(); } this.reportViewer1.RefreshReport(); } } }
Run the RDLC report in Windows Forms application
It is done. Now press Ctrl+F5 to run the project.
Screenshot for the RDLC report demo with barcodes printed in .NET Windows Forms application.
Screenshot for the RDLC report demo with barcodes printed in .NET Windows Forms application.
Tutorial 2: How to print barcodes from DataTable in RDLC report using C#?
Add data source
Add a new item "DataSet1.xsd" to the project.
Add a new data table to "DataSet1.xsd". By default, the table name is "DataTable1".
Add a new column to the data table "DataTable1" with column name "ID" and set its property Data Type to
Add another column to the table with name "Barcode" and set its property Data Type to
Finally, the data table "DataTable1" contains two columns as below.
Add a new data table to "DataSet1.xsd". By default, the table name is "DataTable1".
Add a new column to the data table "DataTable1" with column name "ID" and set its property Data Type to
System.String.
Add another column to the table with name "Barcode" and set its property Data Type to
System.Byte[].
Finally, the data table "DataTable1" contains two columns as below.
Design RDLC report template
Add a new Report item "Report1.rdlc" to the project.
And change property "Copy to Output Directory" to
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "DataTable1" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ID" and "Barcode" to this Table in the report designer.
Insert an image to Barcode field, and set image properties as below.
In "Image Properties", select the image source to Database and use MIME type
Finally, adjust table row height and column width to the appropriate size.
And change property "Copy to Output Directory" to
Copy if newer.
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "DataTable1" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ID" and "Barcode" to this Table in the report designer.
Insert an image to Barcode field, and set image properties as below.
In "Image Properties", select the image source to Database and use MIME type
image/png for field Barcode.
Finally, adjust table row height and column width to the appropriate size.
Add controls to Windows Form
Double click "Form1.cs" to open the designer and add "ReportViewer" control to the form.
Choose report OnBarcodeRDLCWinFormgsDemo.Report1.rdlc for this viewer control.
Select "View Code" from the menu to open the Form1.cs window.
Replace ALL contents in the Form1.cs file by following codes.
Choose report OnBarcodeRDLCWinFormgsDemo.Report1.rdlc for this viewer control.
Select "View Code" from the menu to open the Form1.cs window.
Replace ALL contents in the Form1.cs file by following codes.
Form1.cs
using Microsoft.Reporting.WinForms; using OnBarcode.Barcode.RDLC; 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 OnBarcodeRDLCWinFormsDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Prepare report data. DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(String)); dt.Columns.Add("Barcode", typeof(byte[])); String id1 = "000001"; String id2 = "000002"; // Create data for the 1st row in the table. Linear linear = new Linear(); linear.Type = BarcodeType.CODE128; linear.BarcodeWidth = 600; linear.BarcodeHeight = 100; linear.OutputFileFormat = FileFormat.PNG; linear.Data = id1; byte[] imgData = linear.drawBarcodeAsBytes(); dt.Rows.Add(id1, imgData); // Create data for the 2nd row in the table. linear.Data = id2; imgData = linear.drawBarcodeAsBytes(); dt.Rows.Add(id2, imgData); // Configure Report this.reportViewer1.LocalReport.ReportPath = "Report1.rdlc"; this.reportViewer1.LocalReport.DataSources.Clear(); this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt)); this.reportViewer1.RefreshReport(); } } }
Run the RDLC report in Windows Forms application
It is done. Now press Ctrl+F5 to run the project.
Screenshot for the RDLC report demo with barcodes printed in .NET Windows Forms application.
Screenshot for the RDLC report demo with barcodes printed in .NET Windows Forms application.
How to generate & print barcodes in RDLC files in .NET Windows applications?
This Tutorial Working Environment
- .NET Barcode Generator - Download .NET Barcode Generator for RDLC report files.
- Microsoft Visual Studio 2005 or later version.
- SQL Server 2005 (any edition) with AdventureWorks Database installed.
Generate Barcodes in RDLC
- Create a new .NET Windows Application project. Name the project as "RDLCBarcodeDemo".
- Create a new DataSet.
- Create a new DataSet named "AdventureWorks.xsd".
- In toolbox, drag and drop "TableAdapter" to your created DataSet.
- Create a new connection or use existing connection to SQL Server AdventureWorks Sample Database.
- "Use SQL statements" as the way TableAdapter access the database.
- "SELECT ProductID, Name FROM Production.vProductAndDescription WHERE (CultureID = N'en')" as SQL statements. Then, click "Finish" button.
- Create a new column in the DataTable named "Barcode", by right click "vProductAndDescription" on the DataSet.
- Change new column "Barcode" data type to "System.Byte[]". The type value is not available to select, you need manully input.
- Save DataSet "AdventureWorks.xsd".
- Create a new Report named "Report1.rdlc".
- In Report Items from Toolbar, insert a Table to the report.
- Add 3 columns in DataSet "AdventureWorks.xsd" to the report table details section. And then, drag an Image item to the last column "Barcode".
- Set image item property "MIMEType" to "image/jpeg".
Set image item property "Source" to "Database".
Set image item property "Value" to "=Fields!Barcode.Value". And save the report. - Open your Windows Form, and insert a Report Viewer control on the form. And choose your created report "Report1.rdlc".
- Add "OnBarcode.Barcode.winforms.dll" to .NET project reference from the "Solution Explorer".
- Copy the following C#.NET code into the method Form1_Load and run the Windows application.








private void Form1_Load(object sender, EventArgs e)
{
// load data to the data table
this.vProductAndDescriptionTableAdapter.Fill(this.AdventureWorks.vProductAndDescription);
// create a linear barcode object
Linear barcode = new Linear();
// set barcode type to Code 128
barcode.Type = BarcodeType.CODE128;
// draw barcodes for each data row
foreach (AdventureWorks.vProductAndDescriptionRow row
in this.AdventureWorks.vProductAndDescription.Rows)
{
// set barcode encoding data value
barcode.Data = row.ProductID.ToString();
// set drawing barcode image format
barcode.Format = System.Drawing.Imaging.ImageFormat.Jpeg;
row.Barcode = barcode.drawBarcodeAsBytes();
}
this.reportViewer1.RefreshReport();
}

.NET RDLC Report Barcode Generator Supported Barcodes
.NET Barcode Control for RDLC Local Report - Barcode Image Generation
- 1D / Linear Barcodes in RDLC Report:
- 2D / Matrix Barcodes in RDLC Report: Data Matrix in RDLC, PDF-417 in RDLC, QR Code in RDLC , Micro PDF-417 in RDLC , Micro QR Code in RDLC
