How to add Barcode in Local Reports (RDLC) before report rendering stage – .NET Windows Forms Product List Sample with PDF & Excel support

Prerequisites
– Neodynamic Barcode Professional 3.0 (or greater) for Reporting Services
– Microsoft .NET Framework 2.0 (or greater)
– Microsoft Visual Studio 2005 or Visual Studio 2005 Express Editions (Visual Basic 2005 Express, Visual C# 2005 Express, etc). NOTE: For Express Editions, installing the free add-in for ReportViewer is mandatory.
– Microsoft SQL Server 2005 (any version) with AdventureWorks Database sample installed
In the following Step-By-Step Guide we’ll create a local report (RDLC file) which features barcoding capabilities by using Barcode Professional for Reporting Services.

Follow these steps:
– Open Visual Studio and create a new Windows Forms application naming it LocalReportWithBarcode.
Report’s Data Source settings
Data can be supplied to Local Reports either as custom Objects or as ADO.NET DataTables. In this guide we’ll be exploring each data source scenario in details. So please choose the data source scenario you want to use first and then follow the steps for each of them:
+ Using ADO.NET DataTables as data source of my local report
+ Using Custom Objects as data source of my local report

Using ADO.NET DataTables as data source of my local report
Follow these steps:
– Add a new DataSet item to the project and name it AdventureWorks.xsd


Adding a Typed DataSet

– Add a new TableAdapter to the DataSet (Right-clicking on the DataSet design surface and selecting Add > TableAdapter… or by dragging & dropping a TableAdapter from the Toolbox).


Adding a TableAdapter to the AdventureWorks DataSet

Follow the wizard steps. In the first step, please create a connection to the AdventureWorks SQL Server Database sample and click Next. In the second step, choose “Use SQL statements” and click Next. After that, please enter the following SQL Statement:

SELECT ProductID, Name, ProductModel FROM Production.vProductAndDescription WHERE (CultureID = N’en’)


Specifying a SQL Statement that returns AdventureWorks Product info

– After that, add a new custom Column to the DataTable just created and name it Barcode as is shown in the following figure


Adding a new Column to the DataTable for barcoding purpose

– Change the data type of the Barcode column to System.Byte[] (Array of Byte). NOTE: the System.Byte[] data type is not listed and thus why you must type it manually.


Setting up the Barcode Column to System.Byte[] data type

– Save the AdventureWorks.xsd file.
– Now add a new Report item to the project.


Adding a local report (RDLC) to the project

– The data source for the report should look like the following figure. NOTE: You can display Data Sources Window by selecting Data menu and then Show Data Sources (Shift+Alt+D)


The DataSet is available to be used in the report

– Please design the report so it looks like the following figure.


The barcode report layout

The report features a Table item with 3 columns:

1º Column: It holds an Image item. Drag & drop an Image item into it and set its properties as is shown in the following figure. Notice that Value property of the Image item is bound to the Barcode column.


The Image item’s properties. The Value property is bound to Barcode Column.

2º Column: It holds the Name field

3º Column: It holds the ProductModel field

– Save the report.
– Now Create/Open a Windows Form at design time and drag & drop a ReportViewer control onto it and set the local report to display.


A ReportViewer control to display the barcode report

– After that, from the Solution Explorer, add a reference to Barcode Professional for Reporting Services assembly (By default it is located in [BarcodeInstallDir]BinFor SSRS 2005Neodynamic.ReportingServices.Barcode.dll)
– Write the following code in the Form_Load event procedure.

Visual Basic .NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
‘Fill the datasource from DB

Me.vProductAndDescriptionTableAdapter.Fill(Me.AdventureWorks.vProductAndDescription)
‘Create an instance of Barcode Professional
Dim bcp As New Neodynamic.ReportingServices.Barcode()
‘Barcode settings
bcp.AutoSize = True
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128
bcp.BarHeight = 0.25F
‘Update DataTable with barcode image
Dim row As AdventureWorks.vProductAndDescriptionRow
For Each row In Me.AdventureWorks.vProductAndDescription.Rows
‘Set the value to encode
bcp.Code = row.ProductID.ToString()
‘Generate the barcode image and store it into the Barcode Column
row.Barcode = bcp.GetBarcodeImagePng()
Next
Me.reportViewer1.RefreshReport()
End Sub

Visual C# .NET
private void Form1_Load(object sender, EventArgs e)
{
//Fill the datasource from DB
this.vProductAndDescriptionTableAdapter.Fill(this.AdventureWorks.vProductAndDescription);
//Create an instance of Barcode Professional
Neodynamic.ReportingServices.Barcode bcp = new Neodynamic.ReportingServices.Barcode();
//Barcode settings
bcp.AutoSize = true;
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128;
bcp.BarHeight = 0.25f;

//Update DataTable with barcode image
foreach(AdventureWorks.vProductAndDescriptionRow row in this.AdventureWorks.vProductAndDescription.Rows)
{
//Set the value to encode
bcp.Code = row.ProductID.ToString();
//Generate the barcode image and store it into the Barcode Column
row.Barcode = bcp.GetBarcodeImagePng();
}

this.reportViewer1.RefreshReport();
}

– That’s it. Run your application. You should get the barcode images displayed on the report.


The local report featuring barcodes generated by Barcode Professional

ReportViewer control lets you to export the displayed report to Acrobat PDF as well as Microsoft Excel XLS. In both cases the barcode images are maintained.


The local report in Acrobat PDF format featuring barcodes generated by Barcode Professional

TIP: How to export to Acrobat PDF the Local Report with Barcode images without Preview
The following walkthrough shows how to programmatically export a local report to PDF without viewing it.

Follow these steps:
+ Add a new Windows Form to the project and drag & drop a Button control onto it as is shown in the following figure.


A simple Windows Form to programmatically export a local report to PDF without viewing it

+ Be sure your project is referencing the following assemblies:

Microsoft.ReportViewer.Common.dll
Microsoft.ReportViewer.WinForms.dll
Neodynamic.ReportingServices.Barcode.dll

+ Double click on the Button control and write the following code in the Button1_Click event procedure.

Visual Basic .NET
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Button1.Click

‘Create an instance of ReportViewer
Dim viewer As New Microsoft.Reporting.WinForms.ReportViewer()
‘Set local report
‘NOTE: LocalReportWithBarcode refers to the namespace for the app.
viewer.LocalReport.ReportEmbeddedResource = “LocalReportWithBarcode.Report1.rdlc”

‘Generate Data Source…
‘Fill the datasource from DB

Dim ta As New AdventureWorksTableAdapters.vProductAndDescriptionTableAdapter()
Dim dt As New AdventureWorks.vProductAndDescriptionDataTable()
ta.Fill(dt)

‘Create an instance of Barcode Professional

Dim bcp As New Neodynamic.ReportingServices.Barcode()
‘Barcode settings

bcp.AutoSize = True
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128
bcp.BarHeight = 0.25F

‘Update DataTable with barcode image
Dim row As AdventureWorks.vProductAndDescriptionRow

For Each row In dt.Rows
‘Set the value to encode
bcp.Code = row.ProductID.ToString()
‘Generate the barcode image and store it into the Barcode Column
row.Barcode = bcp.GetBarcodeImagePng()
Next

‘Create Report Data Source
Dim rptDataSource As New Microsoft.Reporting.WinForms.ReportDataSource(“AdventureWorks_vProductAndDescription”, dt)
viewer.LocalReport.DataSources.Add(rptDataSource)

‘Export to PDF. Get binary content.
Dim pdfContent As Byte() = viewer.LocalReport.Render(“PDF”, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
‘Creatr PDF file on disk

Dim pdfPath As String = “C:tempreportBarcode.pdf”
Dim pdfFile As New System.IO.FileStream(pdfPath, System.IO.FileMode.Create)
pdfFile.Write(pdfContent, 0, pdfContent.Length)
pdfFile.Close()
‘Open PDF file
System.Diagnostics.Process.Start(pdfPath)
End Sub

Visual C# .NET
private void button1_Click(object sender, EventArgs e)
{
//Create an instance of ReportViewer
Microsoft.Reporting.WinForms.ReportViewer viewer = new Microsoft.Reporting.WinForms.ReportViewer();

//Set local report
//NOTE: LocalReportWithBarcode refers to the namespace for the app.
viewer.LocalReport.ReportEmbeddedResource = “LocalReportWithBarcode.Report1.rdlc”;

//Generate Data Source…
//Fill the datasource from DB
AdventureWorksTableAdapters.vProductAndDescriptionTableAdapter ta = new AdventureWorksTableAdapters.vProductAndDescriptionTableAdapter();
AdventureWorks.vProductAndDescriptionDataTable dt = new AdventureWorks.vProductAndDescriptionDataTable();
ta.Fill(dt);

//Create an instance of Barcode Professional
Neodynamic.ReportingServices.Barcode bcp = new Neodynamic.ReportingServices.Barcode();
//Barcode settings
bcp.AutoSize = true;
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128;
bcp.BarHeight = 0.25f;

//Update DataTable with barcode image
foreach(AdventureWorks.vProductAndDescriptionRow row in dt.Rows)
{
//Set the value to encode
bcp.Code = row.ProductID.ToString();
//Generate the barcode image and store it into the Barcode Column
row.Barcode = bcp.GetBarcodeImagePng();
}

//Create Report Data Source
Microsoft.Reporting.WinForms.ReportDataSource rptDataSource = new Microsoft.Reporting.WinForms.ReportDataSource(“AdventureWorks_vProductAndDescription”, dt);
viewer.LocalReport.DataSources.Add(rptDataSource);

//Export to PDF. Get binary content.
string mimeType;
string encoding;
string fileNameExtension;
string[] streams;
Microsoft.Reporting.WinForms.Warning[] warnings;
byte[] pdfContent = viewer.LocalReport.Render(“PDF”, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

//Creatr PDF file on disk
string pdfPath = @”C:tempreportBarcode.pdf”;
System.IO.FileStream pdfFile = new System.IO.FileStream(pdfPath, System.IO.FileMode.Create);
pdfFile.Write(pdfContent, 0, pdfContent.Length);
pdfFile.Close();

//Open PDF file
System.Diagnostics.Process.Start(pdfPath);
}

+ That’s it. Run your project opening the last form and click on the button to generate the PDF version for the barcode report.


The local report featuring barcodes exported to PDF format without previewing it onto the Windows Form

Using Custom Objects as data source of my local report
You can design local reports (RDLC) based on business objects defined in your application. In this scenario, an object data source is simply a collection of arbitrary objects. In this guide, we’ll define a custom object with barcoding support that will be used as data source for our report.

Follow these steps:
– Add a new class item to the project and name it Product.


Adding a Product Class item

The Product class will feature basic info about products such is ID and Name as well as a special property called Barcode which will hold the barcode image for each product object.

Please write the following Product class definition:
Visual Basic .NET
Public Class Product
Dim m_id As String
Dim m_name As String
Dim m_barcode As Byte()

Public Sub New(ByVal id As String, ByVal name As String, ByVal barcode As Byte())
m_id = id
m_name = name
m_barcode = barcode
End Sub

Public Property Id() As String
Get
Return m_id
End Get

Set(ByVal value As String)
m_id = value
End Set
End Property

Public Property Name() As String
Get
Return m_name
End Get

Set(ByVal value As String)
m_name = value
End Set

End Property

Public Property Barcode() As Byte()
Get
Return m_barcode
End Get

Set(ByVal value As Byte())
m_barcode = value
End Set
End Property
End Class

Visual C# .NET
public class Product
{
string m_id;
string m_name;
byte[] m_barcode;
public Product(string id, string name, byte[] barcode)
{
m_id = id;
m_name = name;
m_barcode = barcode;
}
public string Id
{
get
{
return m_id;
}
set
{
m_id = value;
}
}
public string Name
{
get
{
return m_name;
}
set
{
m_name = value;
}
}
public byte[] Barcode
{
get
{
return m_barcode;
}
set
{
m_barcode = value;
}
}
}

– Save the class file and go to Build menu and click on Build Solution
– Add a new Report item to the project.


Adding a local report (RDLC) to the project

– Then, go to Data menu and click on Add new Data Source…


Selecting Object Data Source for the local report

In the Data Source Configuration Wizard choose Object and click Next.


Choosing Product Class as the Object Data Source

Select the class you wish to use as a data source (Product in our case) and click Finish. The public properties of the class now appear in the Data Sources window, where they can be dragged and dropped into the report. NOTE: You can display Data Sources Window by selecting Data menu and then Show Data Sources (Shift+Alt+D)


The Product Class is available to be used in the report

– Please design the report so it looks like the following figure.


The barcode report layout

The report features a Table item with 3 columns:

1º Column: It holds an Image item. Drag & drop an Image item into it and set its properties as is shown in the following figure. Notice that Value property of the Image item is bound to the Barcode column.


The Image item’s properties. The Value property is bound to Barcode Column.

2º Column: It holds the Name field

3º Column: It holds the Id field

– Save the report.
– Create/Open a Windows Form at design time and drag & drop a ReportViewer control onto it and set the local report to display.


A ReportViewer control to display the barcode report

– After that, from the Solution Explorer, add a reference to Barcode Professional for Reporting Services assembly (By default it is located in [BarcodeInstallDir]BinFor SSRS 2005Neodynamic.ReportingServices.Barcode.dll)
– Write the following code in the Form_Load event procedure.

Visual Basic .NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

‘Create an instance of Barcode Professional

Dim bcp = new Neodynamic.ReportingServices.Barcode()
‘Barcode settings

bcp.AutoSize = True
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128
bcp.BarHeight = 0.25F

‘Fill the object datasource.
‘NOTE: You may connect to a SQL Server Database and create Product objects based on data returned from such source.

Dim data As New List(Of Product)
‘Create random product info

Dim prodId As String
Dim prodName As String
Dim prodBarcode As Byte()

Dim i As Integer = 0

For i = 1 To 10
‘Random product info
prodId = Guid.NewGuid().ToString().Substring(0, 10)
prodName = “Product ” + prodId
‘Set the value to encode

bcp.Code = prodId
‘Generate the barcode image and store it into the Barcode Column

prodBarcode = bcp.GetBarcodeImagePng()
‘Add new product
data.Add(New Product(prodId, prodName, prodBarcode))
Next
‘Set object data source

Me.ProductBindingSource.DataSource = data
Me.ReportViewer1.RefreshReport()
End Sub

Visual C# .NET
private void Form1_Load(object sender, EventArgs e)
{
//Create an instance of Barcode Professional
Neodynamic.ReportingServices.Barcode bcp = new Neodynamic.ReportingServices.Barcode();
//Barcode settings
bcp.AutoSize = true;
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128;
bcp.BarHeight = 0.25f;

//Fill the object datasource.
//NOTE: You may connect to a SQL Server Database and create Product objects based on data returned from such source.
List data = new List();

//Create random product info
string prodId;
string prodName;
byte[] prodBarcode;

for (int i = 0; i < 10; i++)
{
//Random product info
prodId = Guid.NewGuid().ToString().Substring(0, 10);
prodName = “Product ” + prodId;
//Set the value to encode
bcp.Code = prodId;
//Generate the barcode image and store it into the Barcode Column
prodBarcode = bcp.GetBarcodeImagePng();

//Add new product
data.Add(new Product(prodId, prodName, prodBarcode));
}

//Set object data source
this.ProductBindingSource.DataSource = data;
this.reportViewer1.RefreshReport();
}

– That’s it. Run your application. You should get the barcode images displayed on the report.


The local report featuring barcodes generated by Barcode Professional

ReportViewer control lets you to export the displayed report to Acrobat PDF as well as Microsoft Excel XLS. In both cases the barcode images are maintained.


The local report in Acrobat PDF format featuring barcodes generated by Barcode Professional

TIP: How to export to Acrobat PDF the Local Report with Barcode images without Preview
The following walkthrough shows how to programmatically export a local report to PDF without viewing it.
+ Add a new Windows Form to the project and drag & drop a Button control onto it as is shown in the following figure.


A simple Windows Form to programmatically export a local report to PDF without viewing it

+ Be sure your project is referencing the following assemblies:

Microsoft.ReportViewer.Common.dll
Microsoft.ReportViewer.WinForms.dll
Neodynamic.ReportingServices.Barcode.dll

+ Double click on the Button control and write the following code in the Button1_Click event procedure.

Visual Basic .NET
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Button1.Click

‘Create an instance of ReportViewer
Dim viewer As New Microsoft.Reporting.WinForms.ReportViewer()

‘Set local report
‘NOTE: LocalReportWithBarcode refers to the namespace for the app.
viewer.LocalReport.ReportEmbeddedResource = “LocalReportWithBarcode.Report1.rdlc”
‘Create an instance of Barcode Professional

Dim bcp As New Neodynamic.ReportingServices.Barcode()
‘Barcode settings

bcp.AutoSize = True
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128
bcp.BarHeight = 0.25F

‘Generate Data Source…
‘NOTE: You may connect to a SQL Server Database and create Product objects based on data returned from such source.
Dim data As New List(Of Product)
‘Create random product info

Dim prodId As String
Dim prodName As String
Dim prodBarcode As Byte()

Dim i As Integer = 0

For i = 1 To 10
‘Random product info
prodId = Guid.NewGuid().ToString().Substring(0, 10)
prodName = “Product ” + prodId
‘Set the value to encode
bcp.Code = prodId
‘Generate the barcode image and store it into the Barcode Column
prodBarcode = bcp.GetBarcodeImagePng()
‘Add new product

data.Add(New Product(prodId, prodName, prodBarcode))
Next

‘Create Report Data Source
Dim rptDataSource As New Microsoft.Reporting.WinForms.ReportDataSource(“LocalReportWithBarcode_Product”, data)
viewer.LocalReport.DataSources.Add(rptDataSource)

‘Export to PDF. Get binary content.
Dim pdfContent As Byte() = viewer.LocalReport.Render(“PDF”, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)

‘Creatr PDF file on disk
Dim pdfPath As String = “C:tempreportBarcode.pdf”
Dim pdfFile As New System.IO.FileStream(pdfPath, System.IO.FileMode.Create)
pdfFile.Write(pdfContent, 0, pdfContent.Length)
pdfFile.Close()

‘Open PDF file
System.Diagnostics.Process.Start(pdfPath)
End Sub

Visual C# .NET
private void button1_Click(object sender, EventArgs e)
{
//Create an instance of ReportViewer
Microsoft.Reporting.WinForms.ReportViewer viewer = new Microsoft.Reporting.WinForms.ReportViewer();

//Set local report
//NOTE: LocalReportWithBarcode refers to the namespace for the app.
viewer.LocalReport.ReportEmbeddedResource = “LocalReportWithBarcode.Report1.rdlc”;
//Create an instance of Barcode Professional
Neodynamic.ReportingServices.Barcode bcp = new Neodynamic.ReportingServices.Barcode();
//Barcode settings
bcp.AutoSize = true;
bcp.Symbology = Neodynamic.ReportingServices.Symbology.Code128;
bcp.BarHeight = 0.25f;

//Generate Data Source…
//NOTE: You may connect to a SQL Server Database and create Product objects based on data returned from such source.
List data = new List();

//Create random product info
string prodId;
string prodName;
byte[] prodBarcode;

for (int i = 0; i < 10; i++)
{
//Random product info
prodId = Guid.NewGuid().ToString().Substring(0, 10);
prodName = “Product ” + prodId;
//Set the value to encode
bcp.Code = prodId;
//Generate the barcode image and store it into the Barcode Column
prodBarcode = bcp.GetBarcodeImagePng();
//Add new product
data.Add(new Product(prodId, prodName, prodBarcode));
}

//Create Report Data Source
Microsoft.Reporting.WinForms.ReportDataSource rptDataSource = new Microsoft.Reporting.WinForms.ReportDataSource(“LocalReportWithBarcode_Product”, data);
viewer.LocalReport.DataSources.Add(rptDataSource);

//Export to PDF. Get binary content.
string mimeType;
string encoding;
string fileNameExtension;
string[] streams;
Microsoft.Reporting.WinForms.Warning[] warnings;

byte[] pdfContent = viewer.LocalReport.Render(“PDF”, null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

//Creatr PDF file on disk
string pdfPath = @”C:tempreportBarcode.pdf”;
System.IO.FileStream pdfFile = new System.IO.FileStream(pdfPath, System.IO.FileMode.Create);
pdfFile.Write(pdfContent, 0, pdfContent.Length);
pdfFile.Close();

//Open PDF file
System.Diagnostics.Process.Start(pdfPath);
}

+ That’s it. Run your project opening the last form and click on the button to generate the PDF version for the barcode report.


The local report featuring barcodes exported to PDF format without previewing it onto the Windows Form

Links:
This Demo
More Demos
Download Barcode Professional for Reporting Services
More Information about Neodynamic Barcode Professional for Reporting Services

Neodynamic
.NET Components & Controls
http://www.neodynamic.com
http://www.barcode-for-reporting-services.com