How to data binding ADO.NET DataSet Access or SQL Server DB to print barcode labels with Zebra ZPL-EPL printers and VB.NET or C# by using ThermalLabel SDK for .NET

Prerequisites
- Neodynamic ThermalLabel SDK 2.0 for .NET
- Microsoft .NET Framework 2.0 (or greater)
- Microsoft Visual Studio 2005 / 2008
- Microsoft Visual Studio 2005 / 2008 Express Editions (VB, C#, J#, and C++)
- Any Zebra Thermal Printer supporting ZPL (Zebra Programming Language) or EPL (Eltron Programming Language)

ThermalLabel SDK supports .NET Data Binding scenarios allowing you to print thermal labels bound to a data source such as custom .NET objects, XML files, Databases, ADO.NET, etc.

In this guide you will learn how to perform data binding with databases like MS Access or SQL Server to print barcode labels with Zebra ZPL or EPL printers by using ThermalLabel SDK for .NET

The following sample features an MS Access Database file (C:\Temp\DatabaseSample.mdb) containing an Employees table. An ADO.NET DataTable as well as a ThermalLabel objects will be used for data binding scenario printing a set of thermal labels for each employee as shown in the following figure.

http://www.neodynamic.com/demo-faq/t...DK-for-NET.jpg

NOTE
The following output is from a ZPL printer. Other printers based on different programming languages like EPL will produce different outputs.

IMPORTANT: To test the sample code you must have installed a Zebra ZPL-based or EPL-based thermal printer.


Follow these steps:
- Download and install latest version of Neodynamic ThermalLabel SDK for .NET
- Open Visual Studio 2005 / 2008 and create a Windows Forms application.
- Add a reference to Neodynamic.SDK.ThermalLabel.dll assembly.
- Download the DatabaseSample.mdb database file and copy it to C:\temp\ folder. The structure of the Employees table in DatabaseSample.mdb file is as follows:

http://www.neodynamic.com/demo-faq/t...oyeesTable.jpg

- Add a button control onto the form and paste the following code in the click event handler of the button:

IMPORTANT NOTICE
Although ThermalLabel SDK supports both ZPL and EPL printers, a label design you write using .NET code like C# or VB.NET will not produce the same output printing under ZPL and EPL printers. If you need to support both printer languages in your project then you will have to design two different labels targeting each printer language separately.

For ZPL-based Printers
Visual Basic .NET
'Define a ThermalLabel object and set unit to cm and label size
Dim tLabel As New ThermalLabel(UnitType.Cm, 6, 0)

'TextItem objects for Employee info...
Dim txt1 As New TextItem(0.75, 0.5, "")
'set font...
txt1.Font.Name = "0"
txt1.Font.CharHeight = 12
'set Data Source field...
txt1.DataField = "Name"

Dim txt2 As New TextItem(0.75, 1, "")
'set font...
txt2.Font.Name = "0"
txt2.Font.CharHeight = 10
'set Data Source field...
txt2.DataField = "Address"

Dim txt3 As New TextItem(0.75, 1.5, "")
'set font...
txt3.Font.Name = "0"
txt3.Font.CharHeight = 10
'set Data Source field...
txt3.DataField = "City"

Dim txt4 As New TextItem(3.25, 1.5, "")
'set font...
txt4.Font.Name = "0"
txt4.Font.CharHeight = 10
'set Data Source field...
txt4.DataField = "State"

Dim txt5 As New TextItem(4, 1.5, "")
'set font...
txt5.Font.Name = "0"
txt5.Font.CharHeight = 10
'set Data Source field...
txt5.DataField = "PostalCode"

'BarcodeItem for PostalCode data field...
Dim bc As New BarcodeItem(0.75, 2, BarcodeSymbology.Postnet, "")
'Set Data Source field...
bc.DataField = "PostalCode"
'Set barcode dimensions
bc.BarHeight = 0.3
bc.BarWidth = 0.05
'Hide human readable text...
bc.DisplayCode = false

'Add items to ThermalLabel object...
tLabel.Items.Add(txt1)
tLabel.Items.Add(txt2)
tLabel.Items.Add(txt3)
tLabel.Items.Add(txt4)
tLabel.Items.Add(txt5)
tLabel.Items.Add(bc)

'Create data source...
Dim employees As New DataTable()
Using conn As New System.Data.OleDb.OleDbConnection("Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\temp\DataBaseSample.mdb")
'open db connection...
conn.Open()
'execute db command...
Using cmd As New System.Data.OleDb.OleDbCommand("SELECT TOP 6 * FROM Employees ORDER BY Name", conn)
Using reader = cmd.ExecuteReader()
employees.Load(reader)
End Using
End Using
End Using

'set data source...
tLabel.DataSource = employees

'Create a PrintJob object
Dim pj As New PrintJob()
'Thermal Printer is connected through USB
pj.PrinterSettings.Communication.CommunicationType = CommunicationType.USB
'Set Thermal Printer resolution
pj.PrinterSettings.Dpi = 203
'Set Thermal Printer language
pj.PrinterSettings.ProgrammingLanguage = ProgrammingLanguage.ZPL
'Set Thermal Printer name
pj.PrinterSettings.PrinterName = "Zebra TLP2844-Z"
'Print ThermalLabel object...
pj.Print(tLabel)

Visual C# .NET
//Define a ThermalLabel object and set unit to cm and label size
ThermalLabel tLabel = new ThermalLabel(UnitType.Cm, 6, 0);

//TextItem objects for Employee info...
TextItem txt1 = new TextItem(0.75, 0.5, "");
//set font...
txt1.Font.Name = "0";
txt1.Font.CharHeight = 12;
//set Data Source field...
txt1.DataField = "Name";

TextItem txt2 = new TextItem(0.75, 1, "");
//set font...
txt2.Font.Name = "0";
txt2.Font.CharHeight = 10;
//set Data Source field...
txt2.DataField = "Address";

TextItem txt3 = new TextItem(0.75, 1.5, "");
//set font...
txt3.Font.Name = "0";
txt3.Font.CharHeight = 10;
//set Data Source field...
txt3.DataField = "City";

TextItem txt4 = new TextItem(3.25, 1.5, "");
//set font...
txt4.Font.Name = "0";
txt4.Font.CharHeight = 10;
//set Data Source field...
txt4.DataField = "State";

TextItem txt5 = new TextItem(4, 1.5, "");
//set font...
txt5.Font.Name = "0";
txt5.Font.CharHeight = 10;
//set Data Source field...
txt5.DataField = "PostalCode";

//BarcodeItem for PostalCode data field...
BarcodeItem bc = new BarcodeItem(0.75, 2, BarcodeSymbology.Postnet, "");
//Set Data Source field...
bc.DataField = "PostalCode";
//Set barcode dimensions
bc.BarHeight = 0.3;
bc.BarWidth = 0.05;
//Hide human readable text...
bc.DisplayCode = false;

//Add items to ThermalLabel object...
tLabel.Items.Add(txt1);
tLabel.Items.Add(txt2);
tLabel.Items.Add(txt3);
tLabel.Items.Add(txt4);
tLabel.Items.Add(txt5);
tLabel.Items.Add(bc);

//Create data source...
DataTable employees = new DataTable();
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\temp\DataBaseSample.mdb"))
{
//open db connection...
conn.Open();
//execute db command...
using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT TOP 6 * FROM Employees ORDER BY Name", conn))
{
using (System.Data.OleDb.OleDbDataReader reader = cmd.ExecuteReader())
{
employees.Load(reader);
}
}
}

//set data source...
tLabel.DataSource = employees;

//Create a PrintJob object
PrintJob pj = new PrintJob();
//Thermal Printer is connected through USB
pj.PrinterSettings.Communication.CommunicationType = CommunicationType.USB;
//Set Thermal Printer resolution
pj.PrinterSettings.Dpi = 203;
//Set Thermal Printer language
pj.PrinterSettings.ProgrammingLanguage = ProgrammingLanguage.ZPL;
//Set Thermal Printer name
pj.PrinterSettings.PrinterName = "Zebra TLP2844-Z";
//Print ThermalLabel object...
pj.Print(tLabel);

For EPL-based Printers
Visual Basic .NET
'Define a ThermalLabel object and set unit to cm and label size
Dim tLabel As New ThermalLabel(UnitType.Cm, 6, 0)

'TextItem objects for Employee info...
Dim txt1 As New TextItem(0.75, 0.5, "")
'set font...
txt1.Font.Name = "1"
txt1.Font.CharHeight = 14
txt1.Font.CharWidth = 8
'set Data Source field...
txt1.DataField = "Name"

Dim txt2 As New TextItem(0.75, 1.1, "")
'set font...
txt2.Font.Name = "1"
txt2.Font.CharHeight = 10
txt2.Font.CharWidth = 6
'set Data Source field...
txt2.DataField = "Address"

Dim txt3 As New TextItem(0.75, 1.5, "")
'set font...
txt3.Font.Name = "1"
txt3.Font.CharHeight = 10
txt3.Font.CharWidth = 6
'set Data Source field...
txt3.DataField = "City"

Dim txt4 As New TextItem(3.25, 1.5, "")
'set font...
txt4.Font.Name = "1"
txt4.Font.CharHeight = 10
txt4.Font.CharWidth = 6
'set Data Source field...
txt4.DataField = "State"

Dim txt5 As New TextItem(4, 1.5, "")
'set font...
txt5.Font.Name = "1"
txt5.Font.CharHeight = 10
txt5.Font.CharWidth = 6
'set Data Source field...
txt5.DataField = "PostalCode"

'BarcodeItem for PostalCode data field...
Dim bc As New BarcodeItem(0.75, 2, BarcodeSymbology.Postnet, "")
'Set Data Source field...
bc.DataField = "PostalCode"
'Set barcode dimensions
bc.BarHeight = 0.3
bc.BarWidth = 0.05
'Hide human readable text...
bc.DisplayCode = false

'Add items to ThermalLabel object...
tLabel.Items.Add(txt1)
tLabel.Items.Add(txt2)
tLabel.Items.Add(txt3)
tLabel.Items.Add(txt4)
tLabel.Items.Add(txt5)
tLabel.Items.Add(bc)

'Create data source...
Dim employees As New DataTable()
Using conn As New System.Data.OleDb.OleDbConnection("Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\temp\DataBaseSample.mdb")
'open db connection...
conn.Open()
'execute db command...
Using cmd As New System.Data.OleDb.OleDbCommand("SELECT TOP 6 * FROM Employees ORDER BY Name", conn)
Using reader = cmd.ExecuteReader()
employees.Load(reader)
End Using
End Using
End Using

'set data source...
tLabel.DataSource = employees

'Create a PrintJob object
Dim pj As New PrintJob()
'Thermal Printer is connected through USB
pj.PrinterSettings.Communication.CommunicationType = CommunicationType.USB
'Set Thermal Printer resolution
pj.PrinterSettings.Dpi = 203
'Set Thermal Printer language
pj.PrinterSettings.ProgrammingLanguage = ProgrammingLanguage.EPL
'Set Thermal Printer name
pj.PrinterSettings.PrinterName = "Zebra GK420t"
'Print ThermalLabel object...
pj.Print(tLabel)

Visual C# .NET
//Define a ThermalLabel object and set unit to cm and label size
ThermalLabel tLabel = new ThermalLabel(UnitType.Cm, 6, 0);

//TextItem objects for Employee info...
TextItem txt1 = new TextItem(0.75, 0.5, "");
//set font...
txt1.Font.Name = "1";
txt1.Font.CharHeight = 14;
txt1.Font.CharWidth = 8;
//set Data Source field...
txt1.DataField = "Name";

TextItem txt2 = new TextItem(0.75, 1.1, "");
//set font...
txt2.Font.Name = "1";
txt2.Font.CharHeight = 10;
txt2.Font.CharWidth = 6;
//set Data Source field...
txt2.DataField = "Address";

TextItem txt3 = new TextItem(0.75, 1.5, "");
//set font...
txt3.Font.Name = "1";
txt3.Font.CharHeight = 10;
txt3.Font.CharWidth = 6;
//set Data Source field...
txt3.DataField = "City";

TextItem txt4 = new TextItem(3.25, 1.5, "");
//set font...
txt4.Font.Name = "1";
txt4.Font.CharHeight = 10;
txt4.Font.CharWidth = 6;
//set Data Source field...
txt4.DataField = "State";

TextItem txt5 = new TextItem(4, 1.5, "");
//set font...
txt5.Font.Name = "1";
txt5.Font.CharHeight = 10;
txt5.Font.CharWidth = 6;
//set Data Source field...
txt5.DataField = "PostalCode";

//BarcodeItem for PostalCode data field...
BarcodeItem bc = new BarcodeItem(0.75, 2, BarcodeSymbology.Postnet, "");
//Set Data Source field...
bc.DataField = "PostalCode";
//Set barcode dimensions
bc.BarHeight = 0.3;
bc.BarWidth = 0.05;
//Hide human readable text...
bc.DisplayCode = false;

//Add items to ThermalLabel object...
tLabel.Items.Add(txt1);
tLabel.Items.Add(txt2);
tLabel.Items.Add(txt3);
tLabel.Items.Add(txt4);
tLabel.Items.Add(txt5);
tLabel.Items.Add(bc);

//Create data source...
DataTable employees = new DataTable();
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider= Microsoft.JET.OLEDB.4.0; Data Source=C:\temp\DataBaseSample.mdb"))
{
//open db connection...
conn.Open();
//execute db command...
using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT TOP 6 * FROM Employees ORDER BY Name", conn))
{
using (System.Data.OleDb.OleDbDataReader reader = cmd.ExecuteReader())
{
employees.Load(reader);
}
}
}

//set data source...
tLabel.DataSource = employees;

//Create a PrintJob object
PrintJob pj = new PrintJob();
//Thermal Printer is connected through USB
pj.PrinterSettings.Communication.CommunicationType = CommunicationType.USB;
//Set Thermal Printer resolution
pj.PrinterSettings.Dpi = 203;
//Set Thermal Printer language
pj.PrinterSettings.ProgrammingLanguage = ProgrammingLanguage.EPL;
//Set Thermal Printer name
pj.PrinterSettings.PrinterName = "Zebra GK420t";
//Print ThermalLabel object...
pj.Print(tLabel);

- Run the sample Windows Forms application and test it.

Links:
This Demo
More Demos
Download ThermalLabel SDK for .NET
More Information about Neodynamic ThermalLabel SDK for .NET


Neodynamic
.NET Components & Controls
http://www.neodynamic.com