Home > .NET Crystal Reports Barcode > .NET, C#, VB.NET Barcode Component for Crystal Reports for ASP.NET

How to Generate Barcode Images using Crystal Reports in ASP.NET Website





Crystal Reports Barcode Generator Library

QR Code, Data Matrix, Code 128, GS1, EAN/UPC. Unicode text encoding in C# Crystal Reports ASP.NET, MVC web application.

Download Now
NuGet Ready
.NET Framework 4.x, 3.x, 2.x




How to create, print barcode in Crystal Reports in C# ASP.NET framework web application?

In this section, we will create a new ASP.NET framework project, add a Crystal Reports report, and print barcodes in the report in web browser. By the end of this tutorial, you will view the printed barcodes in Crystal Reports report in web browser.



Prerequisites

  • BarcodeLib.Barcode.CrystalReports.dll
  • Microsoft .NET Framework 4.x
  • Visual Studio 2022
  • Crystal Reports for Visual Studio 2022 Developer Edition


1. Build a new ASP.NET framework project

  1. Create a new project in Visual Studio 2022
  2. Choose ASP.NET Web Application (.NET Framework) and click Next button.
  3. Enter BarcodeLibCrystalReportsBarcodeAspnetDemo for new Project Name, and choose .NET Framework 4.7.2 for Framework. Click button Create.
  4. Choose Empty project for the new web application.
  5. Now we have created a new ASP.NET framework web project.


2. Add barcode generator and Crystal Reports libraries

Here we will add Crystal Reports library and necessary resource files and BarcodeLib Barcode Generator library dll to the ASP.NET project.

  1. Right-click References in the project Solution Explorer, and choose Add Reference... to open the Reference Manager dialog.
  2. Add all Crystal Reports assemblies to the ASP.NET project.


Copy Crystal Reports web resource files to the project

  1. Find the Crystal Reports web resouce files for web report viewer at C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\Crystal Reports 2011\crystalreportviewers
  2. Copy all contents to the ASP.NET project folder
    {Project Root Folder}\aspnet_client\system_web\4_0_30319\crystalreportviewers13

    You need mannual create the new folders aspnet_client, system_web, 4_0_30319, crystalreportviewers13.


Add Barcode Generator dll

  • Add downloaded BarcodeLib barcode dll BarcodeLib.Barcode.CrystalReports.dll to the project reference.
Now you have added all barcode and Crystal Reports library and report web viewer resource files to the project.



3. Prepare data table for report

  1. Add a new DataSet name DataSet1.xsd to the project
  2. Add a new DataTable named DataTable1 to "DataSet1.xsd".
  3. Add two columns to the data table.
    Column Data with DataType value System.String
    Column Barcode with DataType value System.Byte[]


4. Add and design report

  1. Add a new Crystal Reports reporting item named CrystalReport1.rpt.
  2. In the Crystal Reports Gallery dialog
    Choose Using the Report Wizard for new report document,
    Choose Standard as an Expert.
    Click button OK.
  3. Choose DataTable1 in ADO.NET DataSets and click Next button.
  4. Click button Finish


5. Add, resize barcode in report

  1. Open Field Explorer, drag fileds Data and Barcode to the report Details section.
  2. Select the barcode object in the report, and change the following properties:
    XScaling to 4
    YScaling to 1.5
    Here we will print barcode image with 4 inch in width and 1.5 inch in height in the report.


6. Add web form (aspx) with report

  1. Add a new Web Form item named Default.aspx
  2. Copy and replace the following content to the file Default.aspx in Visual Studio.
    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="Default.aspx.cs" 
        Inherits="BarcodeLibCrystalReportsBarcodeAspnetDemo.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>
        BarcodeLib Crystal Reports Barcode Generator ASP.NET Application
    </title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
        AutoDataBind="True"
        Height="1050px" ReportSourceID="CrystalReportSource1" Width="900px" />
    <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
    <Report FileName="CrystalReport1.rpt">
    </Report>
    </CR:CrystalReportSource>
    </div>
    </form>
    </body>
    </html>
    
  3. Copy and replace the following content to the file Default.aspx.cs in Visual Studio.
    using BarcodeLib.Barcode;
    using BarcodeLib.Barcode.CrystalReports;
    using CrystalDecisions.CrystalReports.Engine;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace BarcodeLibCrystalReportsBarcodeAspnetDemo
    {
        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("Data", typeof(String));
                dt.Columns.Add("Barcode", typeof(byte[]));
                String data1 = "BarcodeLib.com";
                String data2 = "Google.com";
                // Create data for the 1st row in the table.
                LinearCrystal barcode = new LinearCrystal();
                barcode.Type = BarcodeType.CODE128;
                barcode.UOM = UnitOfMeasure.INCH;
                barcode.ImageWidth = 4;
                barcode.ImageHeight = 1.5f;
    
    
                barcode.ResizeImagePaddingTransparent = false;
                barcode.ImageFormat = BLImageFormat.PNG;
    
                barcode.Data = data1;
                byte[] imgData = barcode.drawBarcodeAsBytes();
                dt.Rows.Add(data1, imgData);
    
                // Create data for the 2nd row in the table.
                barcode.Data = data2;
                imgData = barcode.drawBarcodeAsBytes();
                dt.Rows.Add(data2, imgData);
    
                // Create a ReportDocument
                ReportDocument rd = new ReportDocument();
                rd.Load(Server.MapPath("~/CrystalReport1.rpt"));
                rd.SetDataSource(dt);
                this.CrystalReportViewer1.ReportSource = rd;
            }
        }
    }
    


7. Config web application

Copy and replace the following content to the file Web.config in Visual Studio.
<?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>


8. Run and print barcode in report in web browser

Now we have finished the whole ASP.NET project building. Run the web application in Visual Studio. You will view the barcodes printed in the report in web browser.







Create Barcode from database in Crystal Reports for ASP.NET



Prerequisites

  • BarcodeLib.Barcode.CrystalReports.dll
  • Microsoft .NET Framework 2.0 (or later)
  • Crystal Report for Visual Studio .NET 2010

Tutorial

  1. Create a new ASP.NET website.
    • Create a new web site project using "ASP.NET Crystal Reports Web Site" as template, named "BarcodeforCrystalReportsWebSite".
  2. Add a Crystal Report item to the project.
    • Add a new item (Crystal Report), named "CustomerReport.rpt" to the project. view image
    • Select Expert as "Mail Label". Click "OK" button. view image
    • In "Mailing Labels Report Creation Wizard", click "Create New Connection", and expand "ADO.NET". view image
    • In "ADO.NET" form, select "CustomerDataSet.xsd" file in your downloaded package, and click "Finish" button. view image
    • In "Mailing Labels Report Creation Wizard", add table "Customer" under "ADO.NET", to selected table. And click "Next" button. view image
    • Add all three fields "ID", "CustomerId", "CustomerName", to "Fields to Display:". Click "Next". view image
    • Select default label "Return Address (Avery 5267)", and click "Finish" button.
    • Now you can view the report template, and find that field "Barcode" not in the report template. view image
    • Re-arrange the report template. view image
    • Drag field "Barcode" to the report template. And Right click "Barcode" object, select "Format Object" menu. view image
    • In "Format Editor" form, check "Can Grow" property. view image
  3. Add Crystal Report Viewer to the Default.aspx.
    • Add a reference to BarcodeLib.Barcode.CrystalReports.dll.
    • Add the following code to the Default.aspx.cs file.
using System.Data.OleDb;
using System.Drawing.Imaging;
using BarcodeLib.Barcode.CrystalReports;
using BarcodeLib.Barcode;


protected void Page_Load(object sender, EventArgs e)
{
	//create the database connection. Please change to correct data file (BarcodeDemoData.mdb) path.
	OleDbConnection aConnection = new OleDbConnection(
	  "Provider=Microsoft.Jet.OLEDB.4.0;
	  Data Source=C:/BarcodeLib_NETBarcode_Trial/ReportingData/BarcodeDemoData.mdb");
	aConnection.Open();

	OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT * FROM Customer", aConnection);
	DataSet ds = new DataSet();
	dataAdapter.Fill(ds);

	//Add the Barcode column to the DataSet
	ds.Tables[0].Columns.Add(new DataColumn("Barcode", typeof(byte[])));

	//Create an instance of Linear Barcode
	//Use DataMatrixCrystal for Data Matrix
	//Use PDF417Crystal for PDF417
	//Use QRCodeCrystal for QR Code
	LinearCrystal barcode = new LinearCrystal();
	//Barcode settings
	barcode.Type = BarcodeType.CODE128;
	barcode.BarHeight = 50; //50 pixel
	barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;

	foreach (DataRow dr in ds.Tables[0].Rows)
	{
	  barcode.Data = (int)dr["CustomerId"] + "";
	  byte[] imageData = barcode.drawBarcodeAsBytes();
	  dr["Barcode"] = imageData;
	}

	CrystalReportSource1.ReportDocument.Load(Server.MapPath("CustomerReport.rpt"));
	CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables[0]);
	CrystalReportSource1.DataBind();

}
3. Run the project.




Other Related .NET Barcode Generator SDKs