How to create, print barcodes in Crystal reports in C# ASP.NET web application?
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
Create a new project with name "OnBarcodeCrystalReportsASPNETDemo".
And choose "Framework" as
Create an empty ASP.NET web application with default settings.
Now, all auto-generated files could be found in the solution explorer.
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
Path of the destination folder
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.
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#?
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[].
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
Select "Product" in ADO.NET DataSets and press
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.
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.
Add Web Form to ASP.NET project
Add a new Web Form item "Default.aspx" to the ASP.NET project
Add a CrystalReportViewer control
Replace behind code class "Default.aspx.cs" by following codes.
Top
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.BarcodeWidth = 500; linear.BarcodeHeight = 100; 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=\"Web\" /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.
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#?
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
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.
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.
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.
Replace behind code class "Default.aspx.cs" by following codes.
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.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 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=\"Web\" /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.
Screenshot for the Crystal Reports report with barcode demo in web browser in C# ASP.NET application.
