Generate QR Code in Crystal Report using C# and VB.Net in ASP.Net

on Oct 29, 2019 12:50 AM

generate qr code on crystal reports in c#


thank you

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Oct 29, 2019 07:45 AM

Hi anirudhp,

Using the below article i have created the sample.

Display image from database in Crystal Report in ASP.Net using C# and VB.Net

Dynamically generate and display QR code Image in ASP.Net

Check this example. Now please take its reference and correct your code.


<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />



using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using QRCoder;


Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO
Imports CrystalDecisions.CrystalReports.Engine
Imports QRCoder



protected void Page_Load(object sender, EventArgs e)
    ReportDocument crystalReport = new ReportDocument();
    Customers dsCustomers = GetData("SELECT TOP 10 CustomerID,Freight FROM Orders");
    CrystalReportViewer1.ReportSource = crystalReport;

private Customers GetData(string query)
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
        using (SqlDataAdapter sda = new SqlDataAdapter())
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
                dsCustomers.Tables["Table"].Columns.Add(new DataColumn("QRCode", typeof(byte[])));
                foreach (DataRow dr in dsCustomers.Tables["Table"].Rows)
                    dr["QRCode"] = GenerateQrCode(dr["CustomerID"].ToString());
                return dsCustomers;

private byte[] GenerateQrCode(string qrmsg)
    string code = qrmsg;
    QRCodeGenerator qrGenerator = new QRCodeGenerator();
    QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
    System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
    imgBarCode.Height = 150;
    imgBarCode.Width = 150;
    using (Bitmap bitMap = qrCode.GetGraphic(20))
        using (MemoryStream ms = new MemoryStream())
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteImage = ms.ToArray();
            return byteImage;


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim crystalReport As ReportDocument = New ReportDocument()
    Dim dsCustomers As Customers = GetData("SELECT TOP 10 CustomerID,Freight FROM Orders")
    CrystalReportViewer1.ReportSource = crystalReport
End Sub

Private Function GetData(ByVal query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As SqlCommand = New SqlCommand(query)
    Using con As SqlConnection = New SqlConnection(conString)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dsCustomers As Customers = New Customers()
                dsCustomers.Tables("Table").Columns.Add(New DataColumn("QRCode", GetType(Byte())))
                For Each dr As DataRow In dsCustomers.Tables("Table").Rows
                    dr("QRCode") = GenerateQrCode(dr("CustomerID").ToString())
                Return dsCustomers
            End Using
        End Using
    End Using
End Function

Private Function GenerateQrCode(ByVal qrmsg As String) As Byte()
    Dim code As String = qrmsg
    Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
    Dim qrCode As QRCodeGenerator.QRCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q)
    Dim imgBarCode As System.Web.UI.WebControls.Image = New System.Web.UI.WebControls.Image()
    imgBarCode.Height = 150
    imgBarCode.Width = 150
    Using bitMap As Bitmap = qrCode.GetGraphic(20)
        Using ms As MemoryStream = New MemoryStream()
            bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
            Dim byteImage As Byte() = ms.ToArray()
            Return byteImage
        End Using
    End Using
End Function