Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Generating labels with barcode in C# using Crystal Reports

0.00/5 (No votes)
21 May 2014 2  
Generating barcode labels in C# with the help of Crystal Reports for printing.

Barcodes Generator Application

Introduction

A Barcode Label System is mostly used in a POS software. I tried Googling and searching in CodeProject for barcode generators, but no result.. So I created an application in C# with Crystal Reports to create printable and scannable barcodes..

This article covers:

  • Integration of Crystal Reports in C#.
  • Using Dataset in Crystal Reports.
  • Generating Barcodes in Crystal Reports.
  • Formatting Detail Section for Multiple columns.
  • Creating a Barcode Label System with less code work.

~Barcode Label System in C#~

Sections:

  • Creating a Project in C#.
  • Adding a Crystal Report to the Project.
  • Formatting the Details Section for Multiple Columns and the Page Properties
  • Adding Details to the Form
  • Adding a Dataset to the Project.
  • Combining the Dataset with Crystal Reports.
  • Adding Details to the Report.
  • Alignment of the Detail
  • Intro to Barcodes.
  • Adding a Barcode to the Report.
  • The Coding
  • FAQs
  • Troubleshooting Errors..

Creating a Project in C#

First open Visual Studio and create a Project with the desired framework..

01 - Create Project

Click OK after filling the details.

Adding a Crystal Report to the Project

02 - Add Item

01 - Create Project

03 - Blank Report

05 - Alignment of Report

  • Right-click the project and navigate to -> Add -> New Item...
  • Select Crystal Report and specify a name for it.. Let's say 'BarcodeReport.rpt'. And click Add.
  • Select Blank Report and click OK
  • Make it Look like this (don't change the detail's height):

Formatting the Details Section for Multiple Columns

06 - section expert

07 -MultipleColumns

08-Page Details

09 - Page Setup

10-Page Setup Units

  • In the Crystal Reports menu, navigate to -> Report -> Section Expert.
  • Select Details and check 'Format with Multiple Columns (Until that you will not see Layout Tab)'.
  • Navigate to Layout tab and fill the details as needed..
  • In the Crystal Reports menu, navigate to -> Design -> Page Setup...
  • Fill the details as needed.. and press OK.

First Run

Press Debug (F5) and make sure your project runs with an Empty Form... If there is an error then follow the procedure below or skip the next procedure:

Like: Missing Assembly Reference

  • Open the properties of the project.
  • If the target framework is in .NET Framework 4 client, then change it to .NET Framework 4, and click Yes.

Hit Debug(F5), the error should be rectified.. If not comment..

Adding Details to the Form

  • Make basic changes to the Form.
  • Drag and drop the Crystal Report Viewer in the form.
  • Make changes to the Report Viewer
  • Click on the small button and choose the report and hit OK.

Adding a Dataset to the Project

You might have noticed a separate column for Barcode and Shop Details, I will talk about this in the FAQ section below..

  • Open Add Item as you did for Crystal Report.
  • Select dataset, give a name, and press Add. Let's give a name 'Barcodes'.
  • Drag and drop a DataTable and rename the Datatable and add columns to it.
  • Add the following columns

Combining the Dataset with Crystal Reports

  • Open Database Expert in the Report.
  • Add the created Dataset to the Report
  • Click OK.
  • Now you can see the details of the DataSet in the database field.

Adding details to the Report

  • Drag and drop the details in the report.

Alignment of the Detail

  • Make the details somewhat look like this:

Intro to Barcodes

Before we generate a barcode we should now what a barcode is and how to use it... Try searching the internet for more details.

Note

We have to Declare the Starting Point and Ending Point to a Barcode so that the scanner can know where the barcode starts and stops. In this article, I am going to use 'Code39' Font(IDAutomationHC39M) a true-type font. The start and stop can be denoted by '*(The Value)*'. Below is an example.

An Example to Generate a Code39 Font

Let the value to encode be ABC123. The barcode must have the value '*ABC123*', where (*) is the start and stop point.. You can get more details here.

Adding Barcode to the Report

There is a link above to download the font file (which is also included in the source code and the demo).

  • Add the barcode data field to the report and arrange it and select the font as shown below.
  • Save the report and close it..

The Coding

This application uses a simple code and is easily translatable to other .NET languages..

Open the Form and use it in the Form_Load event.

Barcodes barcodeDetails = new Barcodes();
DataTable dataTable = barcodeDetails._Barcodes;

BarcodeReport Report = new BarcodeReport();
int blank_labels = 0;
int numberofLabel = 6;
for (int i = 0; i < numberofLabel; i++)
{
    DataRow drow = dataTable.NewRow();
    string P_name = "DETAIL" + i.ToString();
    if (blank_labels <= i)
    {
        drow["Barcode"] = "*";
        drow["Barcode"] += P_name;
        drow["Barcode"] += "*";

        drow["ProductId"] = P_name;
        drow["Product Name"] = "Details of " + i.ToString();
        drow["Cost"] = "Rs 110" + i.ToString() + "/-";
        drow["Code"] = "ABCDE" + i.ToString();
        drow["ShopName"] = "Shop Name";
    }
    dataTable.Rows.Add(drow);
}

Report.Database.Tables["Barcodes"].SetDataSource((DataTable)dataTable);


crystalReportViewer1.ReportSource = Report;
crystalReportViewer1.Refresh();

Explanation

The above code is easily understandable..

  • An integer to tell the number of blank labels so that we could leave the details to Empty.
  • An integer which tells the total number labels to generate..
  • A for loop to loop the data..
  • A data row which uses the created DataTable (dataset).
  • Check about the blank labels.. (number of labels).
  • Add details to the datarow, with start and stop for the barcode field.
  • Add the datarow to the DataTable.
  • At last, add it to the report and refresh the Report Viewer.

And at last you will get something like this:

FAQ

Why am I unable to see the barcode in the Report Viewer? Most often we use Open Type font (.otf), use True Type Font (.ttf) instead.

Why is shop name used in the dataset, why not in the report like parameters? Well in the case of blank labels we cannot print anything in it.

I get an error like:

What to do with this? If you are using .NET 4.0, some may experience an error like this. Replace the app.config file with the below lines:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime Version="v4.0" sku=".NETFramework,Version=v4.0" version="v4.0"/>
  </startup>
</configuration>

The error must be rectified..

Troubleshooting

If you have any questions, leave a comment, I will try to respond as quickly as possible...

Points of Interest

In this article I hope you have understood the usage of start and stop in barcodes, and generating a barcode label system.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here