How to create, print barcodes in SAP Crystal Reports for ASP.NET applications?


How to Generate Linear and 2D Barcodes in Crystal Report

Generate, Create, Print, & Draw Linear, 2D Bar Codes in Crystal Reports for .NET

  • Add robust barcodes generation capabilities into Crystal Reports for .NET
  • Print dynamic QR Code, Data Matrix, and PDF-417 in report with built-in features
  • Supports Code 39, Code 128, EAN-128, EAN-8, EAN-13, UPC-A, UPC-E
  • Provide detailed guide and complete code for VB and C# programmers
  • Capable of encoding barcode with JPEG, PNG, BMP, or GIF image file
  • Completely coded in C#.NET by strong named assemblies
  • Compatible with latest barcode symbology ISO Standards
  • Mature & reliable .NET Barcode Generating Components since 2004
Barcode Generator for Crystal Reports for .NET is a .NET component developed to generate 20+ linear & matrix barcode in Crystal Report. The generator is a control library with barcode generation capabilities. The control could be installed and used easily in Crystal Reports for .NET with Xcopy.
This page will show you how to create, print barcodes in Crystal Reports in ASP.NET web form application using OnBarcode Barcode Generator for Crystal Reports for C# library.
  • Tutorial 1: Print barcodes in report from database
  • Tutorial 2: Print barcodes in report from DataTable


How to create, print barcodes in Crystal Reports in ASP.NET web application?

  1. Download Crystal Reports Barcode Generator library
  2. Install Crystal Reports Barcode Generator dll file into Visual Studio ASP.NET project
  3. Start to Create










How to create, print barcodes in Crystal reports in C# ASP.NET web application?

Top

Create new ASP.NET project

Start Visual Studio 2022 and select "Create a new project".


Select ASP.NET Web Application (.NET Framework) in the dialog and then press Next button.


Create a new project with name "OnBarcodeCrystalReportsASPNETDemo".
And choose "Framework" as .NET Framework 4.7.2.


Create an empty ASP.NET web application with default settings.


Now, all auto-generated files could be found in the solution explorer.




Add project NuGet packages and libraries

Right-click "References" in the Solution Explorer, and select "Add Reference" to open the Reference Manager dialog.


Add all necessary Crystal Reports assemblies to the ASP.NET project.


Note: MUST install "Crystal Reports for Visual Studio 2022 Developer Edition" to use these extensions.

Set up crystal report's Viewers Virtual Directory to ensure that reports are rendered correctly.

Create the destination folder manually if the folder does not exist and then copy the entire "crystalreportviewers" folder to the root directory of this project.

Path of the source folder
C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\Crystal Reports 2011\crystalreportviewers


Path of the destination folder
{ProjectDir}\aspnet_client\system_web\4_0_30319\crystalreportviewers13


Right-click "References" in the Solution Explorer, and select "Manage NuGet Packages".


Select "Browse" and use the search control to find OnBarcode.Crystal.Reports package from the package source "nuget.org".


Choose the version 10.0.1 or later to install the OnBarcode.Crystal.Reports 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#?

Top

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 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[]. Here we will print and insert barcodes into this column.




Finally, the data table "Product" contains three columns as below.




Design Crystal report layout

Add a new Crystal Reports item "CrystalReport1.rpt" to the project.


Select Using the Report Wizard option in the "Crystal Reports Gallery" dialog, and choose Standard in the list box.


Select "Product" in ADO.NET DataSets and press Finish to end the wizard.


Now, the auto-generated file could be found in the solution explorer.


Drag fields "ProductID", "Name" and "Barcode" to the report as below to display.




Adjust printed barcode image size

Open file CrystalReport1.rpt in Visual Studio

Select the barcode object in the report, and change the following properties.
  • Set XScaling to 5
  • Set YScaling to 2


Right click barcode object, and choose Format Object. Go to tab Picture.

Make sure the size settings are correct.





Add Web Form to ASP.NET project

Add a new Web Form item "Default.aspx" to the ASP.NET project


Add a CrystalReportViewer control CrystalReportViewer1 with report source CrystalReportSource1 to the Form (id=form1) in the ASPX file.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="OnBarcodeCrystalReportsASPNETDemo.Default" %>
<%@ Register TagPrefix="CR" Namespace="CrystalDecisions.Web" Assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OnBarcode Crystal Reports ASP.NET Demo (with AdventureWorksLT)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True"
Height="1039px" ReportSourceID="CrystalReportSource1" Width="901px" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport1.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>


Replace behind code class "Default.aspx.cs" by following codes.

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
using OnBarcode.Barcode.CrystalReports;
namespace OnBarcodeCrystalReportsASPNETDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Create a ReportDocument
            ReportDocument rd = new ReportDocument();
            rd.Load(Server.MapPath("~/CrystalReport1.rpt"));
            rd.SetDataSource(GetData());
            this.CrystalReportViewer1.ReportSource = rd;
        }
        private DataTable GetData()
        {
            // Get data by using TableAdapter
            DataSet1TableAdapters.ProductTableAdapter adapter = new DataSet1TableAdapters.ProductTableAdapter();
            DataTable dt = adapter.GetData();
            Linear linear = new Linear();
            linear.Type = BarcodeType.CODE128;
            linear.UOM = UnitOfMeasure.INCH;
            linear.BarcodeWidth = 5;
            linear.BarcodeHeight = 2;
            linear.AutoResizePaddingTransparent = false;
            foreach (DataSet1.ProductRow row in dt.Rows)
            {
                linear.Data = row.ProductID.ToString();
                linear.OutputFileFormat = FileFormat.PNG;
                row.Barcode = linear.drawBarcodeAsBytes();
            }
            return dt;
        }
    }
}




Web.config configuration file

Replace ALL contents in Web.config file by following codes.

<?xml version="1.0" encoding="utf-8"?>

<configuration>
	<configSections>
		<sectionGroup name="businessObjects">
			<sectionGroup name="crystalReports">
				<section name="rptBuildProvider" type="CrystalDecisions.Shared.RptBuildProviderHandler, CrystalDecisions.Shared, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304, Custom=null"/>
			</sectionGroup>
		</sectionGroup>
	</configSections>
	<appSettings>
		<add key="CrystalImageCleaner-AutoStart" value="true"/>
		<add key="CrystalImageCleaner-Sleep" value="60000"/>
		<add key="CrystalImageCleaner-Age" value="120000"/>
	</appSettings>
	<system.web>
		<compilation debug="true" targetFramework="4.7.2">
			<assemblies>
				<add assembly="CrystalDecisions.CrystalReports.Engine, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.ReportSource, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.Shared, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
			</assemblies>
			<buildProviders>
				<add extension=".rpt" type="CrystalDecisions.Web.Compilation.RptBuildProvider, CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
			</buildProviders>
		</compilation>
		<httpRuntime targetFramework="4.7.2" />
		<authentication mode="Windows"/>
		<httpHandlers>
			<add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
		</httpHandlers>
	</system.web>
	<system.webServer>
		<validation validateIntegratedModeConfiguration="false"/>
		<handlers>
			<add name="CrystalImageHandler.aspx_GET" path="CrystalImageHandler.aspx" verb="GET" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode"/>
		</handlers>
	</system.webServer>
	<businessObjects>
		<crystalReports>
			<rptBuildProvider>
				<add embedRptInResource="true"/>
			</rptBuildProvider>
		</crystalReports>
	</businessObjects>
	<system.codedom>
		<compilers>
			<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
			<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
		</compilers>
	</system.codedom>
	<connectionStrings>
		<add name="AdventureWorksLT2019ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=AdventureWorksLT2019;Integrated Security=True"
		providerName="System.Data.SqlClient" />
	</connectionStrings>
</configuration>




Run the Crystal Reports report in ASP.NET application

It is done. Now press Ctrl+F5 to run the project.


Screenshot for the Crystal Reports report demo in web browser in C# ASP.NET application.






Tutorial 2: How to create barcodes from DataTable in Crystal report using C#?

Top

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 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

Add a new Crystal Reports item "CrystalReport1.rpt" to the project.


Select "Using the Report Wizard" option in the Crystal Reports Gallery dialog, and choose "Standard" in the list box.


Select "DataTable1" in ADO.NET DataSets and press "Next" button.


Then, press "Finish" to end the wizard.


Now, the auto-generated file could be found in the solution explorer.


Drag fields "ID" and "Barcode" to the report as below to display.




Adjust printed barcode image size

Open file CrystalReport1.rpt in Visual Studio

Select the barcode object in the report, and change the following properties.
  • Set XScaling to 5
  • Set YScaling to 2


Right click barcode object, and choose Format Object. Go to tab Picture.

Make sure the size settings are correct.





Add ASP.NET Web Form

Add a new Web Form item "Default.aspx" to the project


Add a CrystalReportViewer control "CrystalReportViewer1" with report source "CrystalReportSource1" to the Form (id="form1") in the ASPX web file.

File: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="OnBarcodeCrystalReportsASPNETDemo.Default" %>
<%@ Register TagPrefix="CR" Namespace="CrystalDecisions.Web" Assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>OnBarcode Crystal Reports ASP.NET Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True"
Height="1039px" ReportSourceID="CrystalReportSource1" Width="901px" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="CrystalReport1.rpt">
</Report>
</CR:CrystalReportSource>
</div>
</form>
</body>
</html>


Replace behind code class "Default.aspx.cs" by following codes.

File: Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
using OnBarcode.Barcode.CrystalReports;
namespace OnBarcodeCrystalReportsASPNETDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_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.UOM = UnitOfMeasure.INCH;
                linear.BarcodeWidth = 5;
                linear.BarcodeHeight = 2;
                linear.AutoResizePaddingTransparent = false;
                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 a ReportDocument
                ReportDocument rd = new ReportDocument();
                rd.Load(Server.MapPath("~/CrystalReport1.rpt"));
                rd.SetDataSource(dt);
                this.CrystalReportViewer1.ReportSource = rd;
            }
        }
    }
}




Web.config

Copy ALL contents in configuration tag (in red) to the Web.config file.

File: Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
	<configSections>
		<sectionGroup name="businessObjects">
			<sectionGroup name="crystalReports">
				<section name="rptBuildProvider" type="CrystalDecisions.Shared.RptBuildProviderHandler, CrystalDecisions.Shared, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304, Custom=null"/>
			</sectionGroup>
		</sectionGroup>
	</configSections>
	<appSettings>
		<add key="CrystalImageCleaner-AutoStart" value="true"/>
		<add key="CrystalImageCleaner-Sleep" value="60000"/>
		<add key="CrystalImageCleaner-Age" value="120000"/>
	</appSettings>
	<system.web>
		<compilation debug="true" targetFramework="4.7.2">
			<assemblies>
				<add assembly="CrystalDecisions.CrystalReports.Engine, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.ReportSource, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.Shared, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
				<add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
			</assemblies>
			<buildProviders>
				<add extension=".rpt" type="CrystalDecisions.Web.Compilation.RptBuildProvider, CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
			</buildProviders>
		</compilation>
		<httpRuntime targetFramework="4.7.2" />
		<authentication mode="Windows"/>
		<httpHandlers>
			<add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
		</httpHandlers>
	</system.web>
	<system.webServer>
		<validation validateIntegratedModeConfiguration="false"/>
		<handlers>
			<add name="CrystalImageHandler.aspx_GET" path="CrystalImageHandler.aspx" verb="GET" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode"/>
		</handlers>
	</system.webServer>
	<businessObjects>
		<crystalReports>
			<rptBuildProvider>
				<add embedRptInResource="true"/>
			</rptBuildProvider>
		</crystalReports>
	</businessObjects>
	<system.codedom>
		<compilers>
			<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
			<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
		</compilers>
	</system.codedom>
</configuration>




View the barcode report in web browser

It is done. Now press "Ctrl+F5" to run the project. It is done. Now press Ctrl+F5 to run the project.


Screenshot for the Crystal Reports report with barcode demo in web browser in C# ASP.NET application.




































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.