.NET Barcode Control for RDLC Reports Tutorial

Generate and Print Barcodes in RDLC Report files in .NET Windows Forms Applications

Add & insert high quality linear and 2D barcodes to RDLC report files

  • Easy to install & integrate Barcode Control DLL for RDLC
  • Strong-named and signed assembly for .NET 2.0, 3.0, or greater
  • Compatible with SQL Server 2005 and 2008 with Adventure Works sample database installed
  • Easy to be used in C#, VB.NET, Managed C++ and Borland Delphi for .NET
  • Barcodes generated are compatible with latest barcode symbology ISO Standards
  • Generate barcodes in image files as well as in the memory
  • Customize barcode image, size, rotation, resolution via VB.NET and C#
  • C# source code is available with purchase of the unlimited developer license

How to create, print barcode in RDLC local reports in console, Windows Forms application?

  1. Download RDLC Reports Barcode Generator C# library
  2. Install C# DLL to create barcodes in RDLC reports in .NET WinForms Projects
  3. Step by Step Tutorial








This document explains how to use C# Barcode Generator to generate and print barcodes on local reports RDLC files.
To order .NET RDLC Barcode Generator, please directly refer to: Purchase .NET Barcode Generator for RDLC License.


How to create, print barcodes in RDLC files in C# Windows applications?

Top

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.




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.




Tutorial 1: How to create barcodes from database in RDLC report using C#?

Top

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 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 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.

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.








Tutorial 2: How to print barcodes from DataTable in RDLC report using C#?

Top

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 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 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.

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.




How to generate & print barcodes in RDLC files in .NET Windows applications?

Top

This Tutorial Working Environment

  1. .NET Barcode Generator - Download .NET Barcode Generator for RDLC report files.
  2. Microsoft Visual Studio 2005 or later version.
  3. SQL Server 2005 (any edition) with AdventureWorks Database installed.

Generate Barcodes in RDLC

  1. Create a new .NET Windows Application project. Name the project as "RDLCBarcodeDemo".



  2. 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".
  3. Create a new Report named "Report1.rdlc".



  4. In Report Items from Toolbar, insert a Table to the report.
  5. Add 3 columns in DataSet "AdventureWorks.xsd" to the report table details section. And then, drag an Image item to the last column "Barcode".



  6. 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.



  7. Open your Windows Form, and insert a Report Viewer control on the form. And choose your created report "Report1.rdlc".



  8. Add "OnBarcode.Barcode.winforms.dll" to .NET project reference from the "Solution Explorer".
  9. 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

Top

.NET Barcode Control for RDLC Local Report - Barcode Image Generation





































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.