How to create, print barcodes in Crystal Reports in C# Windows applications?
Prerequisites
- Visual Studio 2022
- .NET Framework 4.7.2 or higher (supporting .NET Standard 2.0)
- Crystal Reports for Visual Studio 2022 Developer Edition (Service Pack 35 for x64)
- Microsoft Visual C++ 2013 Redistributable (x64)
Create new Windows Forms project
Start Visual Studio 2022 and select "Create a new project".
Select Crystal Reports Application in the dialog and then press
Create a new project with name OnBarcodeCrystalReportsWinFormsDemo
Select
Press
Now, all auto-generated files could be found in the solution explorer.
Select Crystal Reports Application in the dialog and then press
Next button.
Create a new project with name OnBarcodeCrystalReportsWinFormsDemo
Select
Using the Report Wizard option in the Crystal Reports Gallery dialog, and choose Standard in the list box.
Press
Finish to end the wizard. The data source would be chosen later.
Now, all auto-generated files could be found in the solution explorer.
Add library and NuGet packages
Right-click "References" in the Solution Explorer, and select "Manage NuGet Packages".
Select "Browse" and use the search control to find OnBarcode.Crystal.Reports from the package source “nuget.org”.
Choose the version 10.0.1 or later to install the package.
Check "References" in the Solution Explorer to ensure the installation is success.
Select "Browse" and use the search control to find OnBarcode.Crystal.Reports from the package source “nuget.org”.
Choose the version 10.0.1 or later to install the package.
Check "References" in the Solution Explorer to ensure the installation is success.
Tutorial 1: How to create barcodes from database in Crystal report using C#?
Add data source
Add a new item "DataSet1.xsd" to the project.
Insert a TableAdapter from Toolbox to "DataSet1.xsd" and configure it with "TableAdapter Configuration Wizard".
Press
Set Server name to (LocalDB)\MSSQLLocalDB and select the restored database AdventureWorksLT2019. (Try "Test Connection" to ensure the connection is successful.) Then, press
Note:
Backup file of the Microsoft sample database AdventureWorks Lightweight (2019) can be downloaded from below website. https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver17&tabs=ssms
Check the checkbox to verify the connection string and press
Save the connection as AdventureWorksLT2019ConnectionString.
Use "Query Builder" to specify a SQL statement to access the database.
Add table "Product (SalesLT)" in the database and select two fields "ProductID" and "Name"; and then, press
Verify the SQL statement and
Add a new column to the data table "Product" with column name "Barcode" and set its property Data Type to
Finally, the data table "Product" contains three columns as below.
Insert a TableAdapter from Toolbox to "DataSet1.xsd" and configure it with "TableAdapter Configuration Wizard".
Press
New Connection to add a data connection to the database.
Set Server name to (LocalDB)\MSSQLLocalDB and select the restored database AdventureWorksLT2019. (Try "Test Connection" to ensure the connection is successful.) Then, press
OK to create the connection string.
Note:
Backup file of the Microsoft sample database AdventureWorks Lightweight (2019) can be downloaded from below website. https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver17&tabs=ssms
Check the checkbox to verify the connection string and press
Next button to continue.
Save the connection as AdventureWorksLT2019ConnectionString.
Use "Query Builder" to specify a SQL statement to access the database.
Add table "Product (SalesLT)" in the database and select two fields "ProductID" and "Name"; and then, press
OK to finish.
Verify the SQL statement and
Finish the wizard.
Add a new column to the data table "Product" with column name "Barcode" and set its property Data Type to
System.Byte[].
Finally, the data table "Product" contains three columns as below.
Design Crystal report template
Double click "CrystalReport1.rpt" in the Solution Explorer to open the report designer.
Right click the report to open the menu and choose "Database Expert".
Choose "Product" in ADO.NET DataSets.
Drag fields "ProductID", "Name" and "Barcode" to the report as below to display.
Right click the report to open the menu and choose "Database Expert".
Choose "Product" in ADO.NET DataSets.
Drag fields "ProductID", "Name" and "Barcode" to the report as below to display.
Add control and behind C# code to the Form
Double click "Form1.cs" to open the designer and add a
Then, add a
Select "View Code" from the popup menu to open the Form1.cs window.
Replace ALL contents in the Form1.cs file by following codes.
Select the Form and switch to Event tab in the Properties window, and then add Load event "Form1_Load".
ProductTableAdapter control with default name "productTableAdapter1" to the form.
Then, add a
DataSet1 with default name "dataSet11".
Select "View Code" from the popup menu to open the Form1.cs window.
Replace ALL contents in the Form1.cs file by following codes.
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using CrystalDecisions.Windows.Forms; using OnBarcode.Barcode.CrystalReports; namespace OnBarcodeCrystalReportsWinFormsDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.productTableAdapter1.Fill(this.dataSet11.Product); Linear linear = new Linear(); linear.Type = BarcodeType.CODE128; linear.BarcodeWidth = 500; linear.BarcodeHeight = 100; foreach (DataSet1.ProductRow row in this.dataSet11.Product.Rows) { linear.Data = row.ProductID.ToString(); linear.OutputFileFormat = FileFormat.PNG; row.Barcode = linear.drawBarcodeAsBytes(); } // Create Report CrystalReport1 rpt = new CrystalReport1(); // Binding DataTable rpt.SetDataSource(this.dataSet11); // Binding Report this.crystalReportViewer1.ReportSource = rpt; } } }
Select the Form and switch to Event tab in the Properties window, and then add Load event "Form1_Load".
Run the Crystal Reports report in Windows Forms application
It is done. Now press Ctrl+F5 to run the project.
Screenshot for the Crystal Reports report demo with barcodes printed in .NET Windows Forms application.
Screenshot for the Crystal Reports report demo with barcodes printed in .NET Windows Forms application.
Tutorial 2: How to create barcodes from DataTable in Crystal report using C#?
Add data source
Add a new item "DataSet1.xsd" to the project.
Add a new data table to "DataSet1.xsd". By default, the table name is "DataTable1"
Add a new column to the data table "DataTable1" with column name "ID" and set its property Data Type to
Add another column to the table with name "Barcode" and set its property Data Type to
Finally, the data table "DataTable1" contains two columns as below.
Add a new data table to "DataSet1.xsd". By default, the table name is "DataTable1"
Add a new column to the data table "DataTable1" with column name "ID" and set its property Data Type to
System.String.
Add another column to the table with name "Barcode" and set its property Data Type to
System.Byte[].
Finally, the data table "DataTable1" contains two columns as below.
Design report layout
Double click "CrystalReport1.rpt" in the Solution Explorer to open the report designer.
Right click the report to open the menu and choose "Database Expert".
Choose "DataTable1" in ADO.NET DataSets.
Drag fields "ID" and "Barcode" to the report as below to display.
Right click the report to open the menu and choose "Database Expert".
Choose "DataTable1" in ADO.NET DataSets.
Drag fields "ID" and "Barcode" to the report as below to display.
Add Form's event
Add source code for Form1's Load event
Select the Form and switch to Event tab in the Properties window, and then add Load event "Form1_Load".
Form1.cs
using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; using CrystalDecisions.Windows.Forms; using OnBarcode.Barcode.CrystalReports; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace OnBarcodeCrystalReportsWinFormsDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Prepare report data. DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(String)); dt.Columns.Add("Barcode", typeof(byte[])); String id1 = "000001"; String id2 = "000002"; // Create data for the 1st row in the table. Linear linear = new Linear(); linear.Type = BarcodeType.CODE128; linear.BarcodeWidth = 600; linear.BarcodeHeight = 100; linear.OutputFileFormat = FileFormat.PNG; linear.Data = id1; byte[] imgData = linear.drawBarcodeAsBytes(); dt.Rows.Add(id1, imgData); // Create data for the 2nd row in the table. linear.Data = id2; imgData = linear.drawBarcodeAsBytes(); dt.Rows.Add(id2, imgData); // Create Report CrystalReport1 rpt = new CrystalReport1(); // Binding DataTable rpt.SetDataSource(dt); // Binding Report this.crystalReportViewer1.ReportSource = rpt; } } }
Select the Form and switch to Event tab in the Properties window, and then add Load event "Form1_Load".
Run the Crystal report with barcode in Windows Forms application
It is done. Now press Ctrl+F5 to run the project.
Screenshot for the Crystal Reports report demo with barcodes printed in .NET Windows Forms application.
Screenshot for the Crystal Reports report demo with barcodes printed in .NET Windows Forms application.
