Hi makumbi,
Please refer below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="500">
</rsweb:ReportViewer>
Namespaces
C#
using System.IO;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using Microsoft.Reporting.WebForms;
using QRCoder;
VB.Net
Imports QRCoder
Imports System.IO
Imports System.Data
Imports System.Drawing
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Drawing.Imaging
Imports Microsoft.Reporting.WebForms
Code
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"] = GenerateBarcode(((dsCustomers.DataTable1).Rows[i]["Name"].ToString()));
}
return dsCustomers;
}
}
}
}
private byte[] GenerateBarcode(string qrmsg)
{
string barCode = qrmsg;
using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M Free Version", 16);
PointF point = new PointF(2f, 2f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, ImageFormat.Png);
return ms.ToArray();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
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 ReportDataSource = New ReportDataSource("Customers", dsCustomers.Tables(0))
ReportViewer1.LocalReport.DataSources.Clear()
ReportViewer1.LocalReport.DataSources.Add(datasource)
End If
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, "DataTable1")
Dim dt As DataTable = dsCustomers.Tables("DataTable1")
For i As Integer = 0 To (dsCustomers.DataTable1).Rows.Count - 1
dsCustomers.DataTable1.Rows(i)("QrCode") = GenerateBarcode(((dsCustomers.DataTable1).Rows(i)("Name").ToString()))
Next
Return dsCustomers
End Using
End Using
End Using
End Function
Private Function GenerateBarcode(ByVal qrmsg As String) As Byte()
Dim barCode As String = qrmsg
Using bitMap As Bitmap = New Bitmap(barCode.Length * 40, 80)
Using graphics As Graphics = graphics.FromImage(bitMap)
Dim oFont As Font = New Font("IDAutomationHC39M Free Version", 16)
Dim point As PointF = New PointF(2.0F, 2.0F)
Dim blackBrush As SolidBrush = New SolidBrush(Color.Black)
Dim whiteBrush As SolidBrush = New SolidBrush(Color.White)
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height)
graphics.DrawString("*" & barCode & "*", oFont, blackBrush, point)
End Using
Using ms As MemoryStream = New MemoryStream()
bitMap.Save(ms, ImageFormat.Png)
Return ms.ToArray()
End Using
End Using
End Function
Screenshot