How to Create QR Code in RDLC Report in C#

By FoxLearn 7/18/2024 3:34:58 AM   8.41K
Creating a QR code report using RDLC (Report Definition Language Client-side) in a C# Windows Forms application involves a few steps.

How to add QR code in RDLC report C#

To play the demo, you need to install the Report Viewer for Visual Studio, then create a new Windows Forms Application project.

c# rdlc report qr code

Right-click on your project, then select the Manage Nuget Packages => Search and Install QRCoderMicrosoft.ReportingServices.ReportViewerControl.Winforms to your project.

install report viewer for visual studio

You can also install these packages using the NuGet Package Manager in Visual Studio or via the Package Manager Console:

Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms
Install-Package QRCoder

Microsoft.ReportingServices.ReportViewerControl.WinForms: This package provides the ReportViewer control for Windows Forms.

QRCoder: This package helps in generating QR codes.

After you finish installing the library packages, you need to rebuild your project.

Next, Open your form designer, then drag TextBox, Button and Report Viewer controls from the visual studio toolbox to your winform.

Right-click on your project => Add =>New Item =>Data =>DataSet, then enter your DataSet name is ReportData.

dataset datatable c#

Open your DataSet, then add a new DataTable to the DataSet. Note, the Image column has the data type of System.Byte[]

create rdlc report in visual studio

You need to create a new reporting by right-clicking on your project =>Add =>New Item =>Visual C# Items =>Report

Open your report designer, then drag an Image control from the report toolbox to your report.

add image to rdlc report

Next, You need to add a datasource to your local report. The DataSource you will add to the report is the dataset you created initially.

local report image datasource

After finishing the report design, we will add the code to display the QR Code on the Report Viewer.

Adding a Form_Load event handler to your form allows you to initialize your rdlc report.

private void Form1_Load(object sender, EventArgs e)
{
    this.reportViewer1.RefreshReport();
    this.reportViewer1.LocalReport.EnableExternalImages = true;
}

Adding a click event handler to the Generate button allows you to generate QR Code, then display the QR Code in the Local Report.

private void btnGenerate_Click(object sender, EventArgs e)
{
    QRCoder.QRCodeGenerator qRCodeGenerator = new QRCoder.QRCodeGenerator();
    QRCoder.QRCodeData qRCodeData = qRCodeGenerator.CreateQrCode(textBox1.Text, QRCoder.QRCodeGenerator.ECCLevel.Q);
    QRCoder.QRCode qRCode = new QRCoder.QRCode(qRCodeData);
    Bitmap bmp = qRCode.GetGraphic(5);
    using (MemoryStream ms = new MemoryStream())
    {
        bmp.Save(ms, ImageFormat.Bmp);
        ReportData reportData = new ReportData();
        ReportData.QRCodeRow qRCodeRow = reportData.QRCode.NewQRCodeRow();
        qRCodeRow.Image = ms.ToArray();
        reportData.QRCode.AddQRCodeRow(qRCodeRow);

        ReportDataSource reportDataSource = new ReportDataSource();
        reportDataSource.Name = "ReportData";
        reportDataSource.Value = reportData.QRCode;
        reportViewer1.LocalReport.DataSources.Clear();
        reportViewer1.LocalReport.DataSources.Add(reportDataSource);
        reportViewer1.RefreshReport();
    }
}

We will use the QRCoder.dll to create a QR Code. This is an open source written in C#. NET allows you to create QR Codes easily. Moreover, it hasn't any dependencies to other libraries and is available as .NET Framework and .NET Core.

To display QR Code in RDLC Report, you need to generate QR Code into image byte array, then add the image byte array to your data source.

VIDEO TUTORIAL

Related