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
Create a new project in Visual Studio 2022
Choose ASP.NET Web Application (.NET Framework) and click Next button.
Enter BarcodeLibCrystalReportsBarcodeAspnetDemo for new Project Name,
and choose .NET Framework 4.7.2 for Framework. Click button Create.
Choose Empty project for the new web application.
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.
Right-click References in the project Solution Explorer, and choose Add Reference... to open the Reference Manager dialog.
Add all Crystal Reports assemblies to the ASP.NET project.
Copy Crystal Reports web resource files to the project
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
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
Add a new DataSet nameDataSet1.xsd to the project
Add a new DataTable named DataTable1 to "DataSet1.xsd".
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
Add a new Crystal Reports reporting item named CrystalReport1.rpt.
In the Crystal Reports Gallery dialog
Choose Using the Report Wizard for new report document,
Choose Standard as an Expert.
Click button OK.
Choose DataTable1 in ADO.NET DataSets and click Next button.
Click button Finish
5. Add, resize barcode in report
Open Field Explorer, drag fileds Data and Barcode to the report Details section.
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
Add a new Web Form item named Default.aspx
Copy and replace the following content to the file Default.aspx in Visual Studio.
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
Create a new ASP.NET website.
Create a new web site project using "ASP.NET Crystal Reports Web Site" as template, named "BarcodeforCrystalReportsWebSite".
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
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
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.
usingSystem.Data.OleDb;
usingSystem.Drawing.Imaging;
usingBarcodeLib.Barcode.CrystalReports;
usingBarcodeLib.Barcode;
protectedvoidPage_Load(objectsender, EventArgse)
{
//create the database connection. Please change to correct data file (BarcodeDemoData.mdb) path.OleDbConnectionaConnection=newOleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:/BarcodeLib_NETBarcode_Trial/ReportingData/BarcodeDemoData.mdb");aConnection.Open();
OleDbDataAdapterdataAdapter=newOleDbDataAdapter("SELECT * FROM Customer", aConnection);
DataSetds=newDataSet();
dataAdapter.Fill(ds);
//Add the Barcode column to the DataSetds.Tables[0].Columns.Add(newDataColumn("Barcode", typeof(byte[])));
//Create an instance of Linear Barcode//Use DataMatrixCrystal for Data Matrix//Use PDF417Crystal for PDF417//Use QRCodeCrystal for QR CodeLinearCrystalbarcode=newLinearCrystal();
//Barcode settingsbarcode.Type=BarcodeType.CODE128;
barcode.BarHeight=50; //50 pixelbarcode.ImageFormat=System.Drawing.Imaging.ImageFormat.Png;
foreach (DataRowdrinds.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();
}