How to Generate, Print Barcode Label in Crystal Reports using C#
Brief Introduction
-
In our demo dataset "BarcodeDemoData.mdb", there is a table "Customer", with three columns inside: "ID", "CustomerId", "CustomerName".
-
There is a CustomerDataSet.xsd file for "BarcodeDemoData.mdb", which defines all above three columns in Customer table, also define one extra column named "Barcode", with data type "xs:base64Binary".
-
In the following example, we will show you how to display all three column data and also with the extra barcode column which displays Code 128 barcode with "CustomerId" value encoded.
Demonstration
-
Create a new .NET project with "Crystal Reports Application" as template. Name the project as "CrystalReportsBarcode"
-
Create a new report "Using the Report Wizard", and choose "Standard", and click "OK" button.
-
In "Data" form, expand "Create New Connection", and expand "ADO.NET".
-
In "Connection" form, select the "CustomerDataSet.xsd" in your downloaded sample dataset package. Then click "Finish" button.
-
In "Data" form, and add table "Customer". Then click "Next".
-
In "Fields" form, add all three columns in the table "Customer". Click "Finish".
-
In CrystalReport1.rpt, add field "Barcode" to the report Section 3 (Details)
-
In your .NET project solution explorer, add "OnBarcode.Barcode.WinForms.dll" to your project reference.
-
Open your Form1.cs in Design view, double click the form, enter Form1.cs.
-
Copy the following code into the method Form1_Load
private void Form1_Load(object sender, EventArgs e) { OleDbConnection aConnection = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Demo/BarcodeDemoData.mdb"); aConnection.Open(); OleDbDataAdapter dataAdapter = new OleDbDataAdapter("select * from Customer", 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[]))); Linear barcode = new Linear(); barcode.Type = BarcodeType.CODE128; foreach (DataRow dr in ds.Tables[0].Rows) { barcode.Data = (int)dr["CustomerId"] + ""; byte[] imageData = barcode.drawBarcodeAsBytes(); dr["Barcode"] = imageData; } CrystalReport1 rpt = new CrystalReport1(); rpt.SetDataSource(ds); this.crystalReportViewer1.ReportSource = rpt; aConnection.Close(); } -
Run the report.
How to Steam Barcodes in Crystal Reports using C#
- Create a new report in your Crystal Reports
-
In your Crystal Reports report, click menu Insert, click menu OLE Object....
-
Choose Bitmap Image as Object Type, and check Display As Icon. Click "OK".
-
Place the bitmap image icon to the right location you need print barcode in your report. Close the Paint application popped.
-
Right click the bitmap image icon, and click menu Format Graphics...
-
Go to Picture tab, and click the icon next to Graphic Location:
-
In the formula windows, input "http://azuredemo.onbarcode.com/linear.aspx?TYPE=7&DATA=" + ToText({Customer.CustomerId}, "#")
http://azuredemo.onbarcode.com/linear.aspx?TYPE=7&DATA= is OnBarcode Online Barcode Generator Hosting Service URL format, if you are streaming barcodes using Java Barcode Generator Web Application or ASP.NET Barcode Generator Web Application, please change the url.
Customer is the table name, CustomerId is the column name.
Here we are printing Code 128 barcodes on the report with CustomerId as barcode encoding data.
- Save and close the "Format Editor" window.
-
Now you can preview the report with barcode Code128 printed.
C#.NET Barcode Generation Guide for Supported Barcode Images
.NET Barcode Control DLL for C# - Barcodes Generation
- C# Linear Barcode Images:
- C# Matrix Barcode Images: Data Matrix, PDF417, QR Code, Micro PDF417, Micro QR Code
