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
- 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#
- Create a new WinForms project in your Visual Studio, with "Crystal Reports Application" as template.
- Create a new report "Using the Report Wizard", choose "Standard", and click "OK" button.
- In "Data" form, expand "Create New Connection", and expand "ADO.NET".
- In "Connection" form, select the "ProductDataSet.xsd" from your downloaded sample dataset package. Then click "Finish" button.
- In "Data" form, add table "Product" and click "Next".
- In "Fields" form, add all three columns in the table "Product" and click "Finish".
- In CrystalReport1.rpt, drag and drop "Barcode" in the "Field Explorer" to the report Section 3.
- In .NET project "Solution Explorer", add "KeepAutomation.Barcode.Crystal.dll" to your project reference.
- 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#
- Open your Visual Studio and create a new web project with "ASP.NET Crystal Reports Web Site" as template.
- In the pop-up window, choose "Standard", and click "OK" button.
- Then, double click "Create New Connection" and expand "ADO.NET".
- In "ADO.NET" form, select "ProductDataSet.xsd" file in your downloaded package, and click "Finish" button.
- Next, add table "Product" and click "Next".
- In "Fields" form, add all three columns in the table "Product" and click "Finish".
- In CrystalReport1.rpt, add "Barcode" item to the report Section 3.
- Add "KeepAutomation.Barcode.Crystal.dll" to your project reference in "Solution Explorer".
- Right click "Default.aspx" in"Solution Explorer" and choose "View Code".
- 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();