Barcode for Crystal Report > Generate Barcode in C#

How to Create Barcodes in Crystal Reports Using C#.NET

How to Generate 1D and Matrix Barcode in Crystal Reports Using Visual C# Class
KA.Barcode for Crystal Reports Overview
KeepAutomation Barcode Generator for Crystal Reports is the most flexible and powerful barcode generation component that is capable of encoding most linear and 2D barcodes with high quality and high readability in Crystal Reports. In this page, we provide you detail information of generating barcodes in Crystal Reports using C#.NET.
Compatibility & Requirements
  • Development Environments: Visual Studio 2005/2008/2010, Visual C#, Visual Basic.NET, Crystal Reports for .NET (runtime), .NET Framework 2.0, 3.0, 3.5, 4.0
  • Windows Compatibility: Windows Vista/Windows 7/Windows XP/Window Server 2005/2008
Test Environment
  • Microsoft Windows XP
  • Microsoft Visual Studio 2005
  • Visual C#.NET
Generate Barcodes in Crystal Reports in WinForms Using C#

Brief Introduction of Demo Package
In the unzipped package file, open the demo dataset "KACrystalData.mdb". You will see a table named "Product" with three columns inside: "ID", "ProductId", "ProductName".
There is a ProductDataSet.xsd file for "KACrystalData.mdb", which defines all above three columns in "Product" table and an extra column named "Barcode".
How to Generate Barcodes for Crystal Reports in WinForms Using C#
  1. Create a new WinForms project in your Visual Studio, with "Crystal Reports Application" as template.
  2. Create a new report "Using the Report Wizard", choose "Standard", and click "OK" button.
  3. In "Data" form, expand "Create New Connection", and expand "ADO.NET".
  4. In "Connection" form, select the "ProductDataSet.xsd" from your downloaded sample dataset package. Then click "Finish" button.
  5. In "Data" form, add table "Product" and click "Next".
  6. In "Fields" form, add all three columns in the table "Product" and click "Finish".
  7. In CrystalReport1.rpt, drag and drop "Barcode" in the "Field Explorer" to the report Section 3.
  8. In .NET project "Solution Explorer", add "KeepAutomation.Barcode.Crystal.dll" to your project reference.
  9. Open your "Form1.cs", copy the following code into the method Form1_Load and run the report.
C# Sample code
     OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/KACrystalData.mdb");
aConnection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("select * from Product", aConnection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
//add a new column named "Barcode" to the DataSet, the new column data type is byte[]
ds.Tables[0].Columns.Add(new DataColumn("Barcode", typeof(byte[])));
BarCode barcode = new BarCode();
barcode.Symbology = KeepAutomation.Barcode.Symbology.Code39;
foreach (DataRow dr in ds.Tables[0].Rows)
{
barcode.CodeToEncode = (int)dr["ProductId"] + "";
byte[] imageData = barcode.generateBarcodeToByteArray();
dr["Barcode"] = imageData;
}
CrystalReport1 rpt = new CrystalReport1();
rpt.SetDataSource(ds);
this.crystalReportViewer1.ReportSource = rpt;
aConnection.Close();
Generate Barcodes in Crystal Reports in Web Forms Using C#
  1. Open your Visual Studio and create a new web project with "ASP.NET Crystal Reports Web Site" as template.
  2. In the pop-up window, choose "Standard", and click "OK" button.
  3. Then, double click "Create New Connection" and expand "ADO.NET".
  4. In "ADO.NET" form, select "ProductDataSet.xsd" file in your downloaded package, and click "Finish" button.
  5. Next, add table "Product" and click "Next".
  6. In "Fields" form, add all three columns in the table "Product" and click "Finish".
  7. In CrystalReport1.rpt, add "Barcode" item to the report Section 3.
  8. Add "KeepAutomation.Barcode.Crystal.dll" to your project reference in "Solution Explorer".
  9. Right click "Default.aspx" in"Solution Explorer" and choose "View Code".
  10. Copy the following code accordingly and run the project.
C# Sample code
     OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:/KACrystalData.mdb");
aConnection.Open();

OleDbDataAdapter dataAdapter = new OleDbDataAdapter("select * from Product",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
BarCode barcode = new BarCode();
//Barcode settings
barcode.Symbology=KeepAutomation.Barcode.Symbology.Code11;
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;

foreach (DataRow dr in ds.Tables[0].Rows)
{
barcode.CodeToEncode = (int)dr["ProductId"] + "";
byte[] imageData = barcode.generateBarcodeToByteArray();
dr["Barcode"] = imageData;
}
CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport1.rpt"));
CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables[0]);
CrystalReportSource1.DataBind();
CompatibilityTest EnvironmentFor Windows C#For Web C#Using C#Using VB.NETUsing ASP.NETUsing .NET WinformsData Matrix for Crystal ReportPDF417 for Crystal ReportQR Code for Crystal ReportmoreCodabar for Crystal ReportCode 39 for Crystal ReportCode 128 for Crystal ReportEAN-8 for Crystal ReportEAN-13 for Crystal ReportEAN 128 for Crystal ReportIntelligent Mail for Crystal ReportInterleaved 2 of 5 for Crystal ReportISBN for Crystal ReportITF-14 for Crystal ReportRM4SCC for Crystal ReportUPC-A for Crystal ReportUPC-E for Crystal Reportmore
KeepAutomation provides complete barcode automation solutions for add barcode display 2-dimensional, barcode generation capacity api, and barcode mobile applications. We recommend the customers use high quality ASP.NET html5 document viewer, c# pdf sdk, C# merge PDF files, Split PDF in C#, C# View, edit PDF online , C# Convert PDF to Tiff, C# Convert PDF to text, txt, .net imaging sdk, C# PDF metadata edit from RasterEdge.com.