Using C1Barcode in Crystal Reports

Users often times prefer to use C1Barcode in a column in Crystal Report. This bloggives a Step-By-Step to create a Crystal Report, which features barcoding capabilities by using C1Barcode and using them as a data source for the report a Typed DataSet.

Follow these steps:

  • Open Visual Studio and create a new Windows Application naming it CrystalReportBarcodeDataSet.

  • Add a new DataSet item to the project and name it Northwind.xsd

Adding a Typed DataSet

  • Click Add button. After that, right click on the DataSet and select Add > TableAdapter.. Once you do that the TableAdapter Configuration Wizard is automatically launched. Please follow its appropriate steps. In the first step, please create a connection to the C1Nwind.mdb database (this database can be found in the ComponentOne Samples | Common folder) and click Next. In the second step, select save Connection String and click Next.

Following the former steps, choose "Use SQL statements" and click Next. After that, please enter this SQL Statement:

SELECT ProductID, ProductName, UnitPrice FROM Products

Specifying an SQL Statement that returns Northwind Product info

Click 'Finish' to close the Wizard dialog box.

  • Then, add a new custom Column to the DataTable just created and name it Barcode as is shown in the figure below:

Adding a new Column to the DataTable for barcoding purpose

  • Change the data type of the Barcode column to System.Byte[] (Array of Byte).

Setting up the Barcode Column to System.Byte[] data type

  • Save the Northwind.xsd file.

  • Now add a new Crystal Report item to the project and name it CrystalReportBarcode.rpt.

  • Click the Add button. Next, choose "Blank Report" in the Crystal Report Gallery dialog box.

Creating a blank report

Click 'OK'

  • Launch Crystal Report Database Expert by going to Crystal Reports > Database > Database Expert...

Opening Crystal Reports Database Expert dialog box

In the Database Expert dialog box select the Products DataTable (from the left pane) so it appears in the "Selected Tables" list (right pane) as is shown in the following figure.

Choosing the Products DataTable as data source of the Crystal Report

Click the OK button. After that, the Products DataTable should be available in the Field Explorer Window as is shown in the figure below. NOTE: To display the Field Explorer press Ctrl Alt T

The Products DataTable available in the Field Explorer

  • Please design the report so it looks like the following figure. In order to get that done, drag and drop the fields from the Field Explorer onto the report (as is shown in the figure).

The barcode report layout

The most important field in our scenario is Barcode (Please DO NOT change the dimensions of such field on the report surface). Select the Barcode item on the report and right-click it, selecting "Format Object" from the context menu to open the Format Editor dialog box. In that dialog box, please check the 'Can Grow' option and click OK.

  • Save the report.

  • Now, Open MainForm windows form at design time and drag & drop a CrystalReportViewer control onto it.

  • Proceed by adding a reference to C1Barcode assembly from the Solution Explorer: C1.Win.C1Barcode**.dll**

  • Double click on the form to handle the Form_Load event and write the following code in the event procedure.

Visual Basic .NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'Filling Products DataTable from DB

Dim ta As New NothWindTableAdapters.ProductsTableAdapter()

Dim dt As New NothWind.ProductsDataTable()

ta.Fill(dt)

'Create an instance of C1Barcode

Dim barcode As New C1.Win.C1BarCode.C1BarCode()

'Barcode settings

barcode.CodeType = C1.Win.C1BarCode.CodeTypeEnum.Code39

barcode.BarHeight = 30

'Append fictitious Company internal prefix

Dim prodPrefix As String = "12345"

'Update DataTable with barcode image

For Each row As NothWind.ProductsRow In dt.Rows

'Set the value to encode

barcode.Text = prodPrefix & row.ProductID.ToString()

Dim imgbyte As Byte()

'Converting C1Barcode Image to Byte[]

Using ms As New System.IO.MemoryStream()

(barcode.GetImage(System.Drawing.Imaging.ImageFormat.Png)).Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)

imgbyte = ms.ToArray()

End Using

'Generate the barcode image and store it into the Barcode Column

row.Barcode = imgbyte

Next

'Create a report object

'and set its data source with the DataSet

Dim report As CrystalDecisions.CrystalReports.Engine.ReportDocument

report = New CrystalDecisions.CrystalReports.Engine.ReportDocument()

Dim rptFile As String = "..\..\CrystalReportBarcode.rpt"

report.Load(rptFile)

report.SetDataSource(DirectCast(dt, DataTable))

crystalReportViewer1.ReportSource = report

End Sub

Visual C# .NET

private void MainForm_Load(object sender, EventArgs e)

{

//Filling Products DataTable from DB

NothWindTableAdapters.ProductsTableAdapter ta = new NothWindTableAdapters.ProductsTableAdapter();

NothWind.ProductsDataTable dt = new NothWind.ProductsDataTable();

ta.Fill(dt);

//Create an instance of C1Barcode

C1.Win.C1BarCode.C1BarCode barcode = new C1.Win.C1BarCode.C1BarCode();

//Barcode settings

barcode.CodeType = C1.Win.C1BarCode.CodeTypeEnum.Code39;

barcode.BarHeight = 30;

//Append fictitious Company internal prefix

string prodPrefix = "12345";

//Update DataTable with barcode image

foreach (NothWind.ProductsRow row in dt.Rows)

{

//Set the value to encode

barcode.Text = prodPrefix row.ProductID.ToString();

byte[] imgbyte;

//Converting C1Barcode Image to Byte[]

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

{

(barcode.GetImage(System.Drawing.Imaging.ImageFormat.Png)).Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

imgbyte = ms.ToArray();

}

//Generate the barcode image and store it into the Barcode Column

row.Barcode = imgbyte;

}

//Create a report object

//and set its data source with the DataSet

CrystalDecisions.CrystalReports.Engine.ReportDocument report;

report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();

string rptFile = "..\..\\CrystalReportBarcode.rpt";

report.Load(rptFile);

report.SetDataSource((DataTable)dt);

crystalReportViewer1.ReportSource = report;

}

You may run your application now. You should get the barcode images displayed on the report.

The Crystal Report featuring barcodes generated by C1Barcode

Download CrystalReportBarcodeDataSet

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus