Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Crystal Reports is a powerful reporting tool that allows developers to create feature-rich reports for their applications. When it comes to including barcodes in Crystal Reports using C#, it adds a new dimension to data representation, making it easier to manage and track information. In this article, we'll explore the steps to integrate barcodes into Crystal Reports using C#.
Before diving into the implementation, it's essential to select a barcode library that supports Crystal Reports and C#. One popular choice is the IronBarcode for .NET.
IronBarcode is a versatile .NET library that simplifies barcode generation and reading. With IronBarcode, you can effortlessly create various barcodes, including Code 128 and QR codes, by specifying the value to encode. It also supports resizing and customization. On the reading side, IronBarcode can extract barcode data from images or PDFs, making it ideal for inventory management and document tracking. Its user-friendly API ensures quick integration into your projects, and cross-platform support allows seamless development across different .NET versions. Whether you’re a seasoned developer or a beginner, IronBarcode empowers you to work with barcodes efficiently.
Open Visual Studio for creating an ASP.NET Crystal Reports Web Site. I am using Visual Studio 2022. You may use any, but make sure Crystal reports for Visual Studio for that particular version is installed.
Select Project Name, Location & Target Framework. Click on the Create Button. A new Project will be created as shown below.
Before we begin, we need to have a Database. Let's create a new database and a sample table.
The following script will create a new database.
CREATE DATABASE ProductDB;
USE [ProductDB]
GO
/****** Object: Table [dbo].[Products] Script Date: 3/10/2024 2:57:18 PM**/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Products](
[Product_ID] [int] NULL,
[Product_Name] [varchar](100) NULL,
[Product_Price] [decimal](18, 0) NULL,
[Product_Barcode] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Let's Insert Data into this table. We need to create a barcode and Save it in the Product table. Therefore, we need to insert data using C# Code. We need to install IronBarcode Library in our Project to use its features to create barcodes.
To install the IronBarcode library using the NuGet Package Manager Console, follow these steps:
Open the NuGet Package Manager Console. You can find it by going to View -> Other Windows -> Package Manager Console.
In the Package Manager Console, use the following command to install the IronBarcode library:
Install-Package Barcode
Press Enter to execute the command.
Alternatively, you can install IronBarcode library using the Manage NuGet Packages for Solution:
Wait for the NuGet Package Manager to download and install the IronBarcode library and its dependencies. Once the installation is complete, you'll see a confirmation message in the Package Manager Console.
Now, the IronBarcode library is installed in your project, and you can start using its features for barcode generation and reading.
I will generate barcode images, and store them in the database using ADO.NET. The following code will demonstrate the example of generating a barcode in C#.
static void Main(string [] args)
{
var myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128);
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.ResizeTo(600, 300);
SqlConnection cn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;initial catalog=ProductDB ; User ID=sa;Password=123456;Integrated Security=SSPI;");
SqlCommand cmd = new SqlCommand($"INSERT INTO dbo.Products values (77446252, 'Pine Apple Small','100', '{myBarcode.BinaryStream}' )", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
static void Main(string [] args)
{
var myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128);
myBarcode.AddBarcodeValueTextBelowBarcode();
myBarcode.ResizeTo(600, 300);
SqlConnection cn = new SqlConnection("Data Source=localhost\\SQLEXPRESS;initial catalog=ProductDB ; User ID=sa;Password=123456;Integrated Security=SSPI;");
SqlCommand cmd = new SqlCommand($"INSERT INTO dbo.Products values (77446252, 'Pine Apple Small','100', '{myBarcode.BinaryStream}' )", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
Shared Sub Main(ByVal args() As String)
Dim myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128)
myBarcode.AddBarcodeValueTextBelowBarcode()
myBarcode.ResizeTo(600, 300)
Dim cn As New SqlConnection("Data Source=localhost\SQLEXPRESS;initial catalog=ProductDB ; User ID=sa;Password=123456;Integrated Security=SSPI;")
Dim cmd As New SqlCommand($"INSERT INTO dbo.Products values (77446252, 'Pine Apple Small','100', '{myBarcode.BinaryStream}' )", cn)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
End Sub
The above source code generates a barcode, connects to a SQL Server database, inserts data (including the barcode) into the Products table, and then closes the database connection.
var myBarcode = BarcodeWriter.CreateBarcode("77446252", BarcodeWriterEncoding.Code128);
This line creates a barcode using the BarcodeWriter class's CreateBarcode() method. The barcode is generated from the binary data represented by the string "77446252" using the Code 128 encoding format1. The myBarcode variable now holds the generated barcode.
The following barcode will be generated from the above code.
Now, Design the report layout, add database connections, and arrange the necessary fields. If you're new to this, follow the following steps.
Open Field Explorer => Database Field => Database Expert.
Expand Create a new Connection => OLE DB(ADO) => Make New Connection
Select Microsoft OLE DB Data Source for SQL Server. Click Next.
Provide the Server Name, Login Credential, and Database Name as shown below.
Select the Table you want to add. In this case, select the products table.
We have set up a Database Connection. Now, let's set up the Report layout.
Now, I have added a Text Box with the text "Barcode in Crystal report C#". Added text Box, Drag & Drop Product ID, Product Name, and Product Price Field, Product Barcode from database Fields and placed them inside the Box as shown below.
See the Crystal Report Preview in Crystal Report Viewer.
Build and run the project. The Output is as:
I have not downloaded sample dataset package, ensuring I had the correct data file, and proceeded to create a Crystal Report in C# for comprehensive data visualization.
In this way, we can create Bar code in the Crystal Reports Application without downloading the Barcode Font. Similarly, we can also add a QR Code as per your requirement.
In conclusion, integrating barcodes into Crystal Reports using C# is a powerful way to enhance data representation and management. Choosing a reliable barcode library like IronBarcode streamlines the process, offering versatility and ease of use. IronBarcode, with its support for various barcode types, resizing, and customization features, proves to be a valuable asset in barcode generation and reading tasks. The step-by-step guide provided here ensures a seamless implementation process, from selecting the library to designing the Crystal Report layout.
Moreover, IronBarcode, unlocking additional functionalities and support to further enhance their barcode integration experience. This flexibility makes IronBarcode a compelling choice for developers, whether they are working on small-scale projects or enterprise-level applications.
9 .NET API products for your office documents