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

anirudhp
 
on Oct 29, 2019 12:50 AM
Sample_797208.zip
6199 Views

generate qr code on crystal reports in c#

 

thank you

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
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.

HTML

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

Namespaces

C#

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

VB.Net

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

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    ReportDocument crystalReport = new ReportDocument();
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"));
    Customers dsCustomers = GetData("SELECT TOP 10 CustomerID,Freight FROM Orders");
    crystalReport.Database.Tables[0].SetDataSource(dsCustomers.Tables["Table"]);
    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())
            {
                sda.Fill(dsCustomers);
                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;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim crystalReport As ReportDocument = New ReportDocument()
    crystalReport.Load(Server.MapPath("~/CustomerReport.rpt"))
    Dim dsCustomers As Customers = GetData("SELECT TOP 10 CustomerID,Freight FROM Orders")
    crystalReport.Database.Tables(0).SetDataSource(dsCustomers.Tables("Table"))
    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()
                sda.Fill(dsCustomers)
                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())
                Next
                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