Windows Forms: How to generate QR Code in RDLC Report using C#

This post shows you How to generate QR Code in RDLC Report using C# .NET Windows Forms Application.

Sometimes you need to generate qr code on the local report. This article will guide you how to display a qr code on rdlc report in 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 => QRCoder, Microsoft.ReportingServices.ReportViewerControl.Winforms to your project.

install report viewer for visual studioAfter 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 Posts