How to generate QRCode in RDLC report using C# and VB.Net in ASP.Net

Last Reply one month ago By Indresh

Posted one month ago

im generating qrcode in my project and assigning to image, that image i want to come in rdlc report how to fix pls reply

thanks

Posted one month ago

Hi nandish,

I have created sample code by referring the below article which full-fill your requirement.So please refer the below code and modify as per your requirement.

How to create RDLC report step by step in ASP.Net using C# and VB.Net

HTML

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600">
    </rsweb:ReportViewer>
</div>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        Customers dsCustomers = GetData("select TOP 5 * from customers");
        ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}

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, "DataTable1");

                DataTable dt = dsCustomers.Tables["DataTable1"];
                for (int i = 0; i < (dsCustomers.DataTable1).Rows.Count; i++)
                {
                    (dsCustomers.DataTable1).Rows[i]["QrCode"] = GenerateQrCode(((dsCustomers.DataTable1).Rows[i]["Name"].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(sender As Object, e As EventArgs)
	If Not IsPostBack Then
		ReportViewer1.ProcessingMode = ProcessingMode.Local
		ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
		Dim dsCustomers As Customers = GetData("select TOP 5 * from customers")
		Dim datasource As New ReportDataSource("Customers", dsCustomers.Tables(0))
		ReportViewer1.LocalReport.DataSources.Clear()
		ReportViewer1.LocalReport.DataSources.Add(datasource)
	End If
End Sub

Private Function GetData(query As String) As Customers
	Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
	Dim cmd As New SqlCommand(query)
	Using con As New SqlConnection(conString)
		Using sda As New SqlDataAdapter()
			cmd.Connection = con

			sda.SelectCommand = cmd
			Using dsCustomers As New Customers()
				sda.Fill(dsCustomers, "DataTable1")

				Dim dt As DataTable = dsCustomers.Tables("DataTable1")
				For i As Integer = 0 To (dsCustomers.DataTable1).Rows.Count - 1
					(dsCustomers.DataTable1).Rows(i)("QrCode") = GenerateQrCode(((dsCustomers.DataTable1).Rows(i)("Name").ToString()))
				Next

				Return dsCustomers
			End Using
		End Using
	End Using
End Function

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

Screenshot

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108