Click here to Skip to main content
15,884,298 members
Articles / Web Development / HTML

Generating labels with barcode in C# using Crystal Reports

Rate me:
Please Sign up or sign in to vote.
4.77/5 (34 votes)
21 May 2014CPOL5 min read 190.9K   33.9K   61   49
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:

Image 12

Like: Missing Assembly Reference

Image 13

Image 14

Image 15

  • 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

Image 16

Image 17

  • 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

Image 18

Image 19

Image 20

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

Image 21

Image 22

Image 23

Image 24

  • 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

Image 25

  • Drag and drop the details in the report.

Alignment of the Detail

Image 26

  • 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).

Image 27

  • 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.

C#
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:

Image 28

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:

Image 29

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
<?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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
'If you don't explain easily you don't understand it well enough' - My Pal (Albert Einstein)

Comments and Discussions

 
GeneralRe: VS 2013 support Pin
Member 416585117-Nov-14 21:16
Member 416585117-Nov-14 21:16 
GeneralRe: VS 2013 support Pin
Mohammed Owais19-Nov-14 0:15
professionalMohammed Owais19-Nov-14 0:15 
SuggestionFree Barcode Printing Pin
Tammam Koujan12-Nov-14 3:43
professionalTammam Koujan12-Nov-14 3:43 
GeneralRe: Free Barcode Printing Pin
Mohammed Owais12-Nov-14 3:59
professionalMohammed Owais12-Nov-14 3:59 
QuestionBarcode Printing Pin
Member 1122051010-Nov-14 23:43
Member 1122051010-Nov-14 23:43 
AnswerRe: Barcode Printing Pin
Mohammed Owais12-Nov-14 0:45
professionalMohammed Owais12-Nov-14 0:45 
GeneralRe: thank you Pin
Mohammed Owais30-Sep-14 3:30
professionalMohammed Owais30-Sep-14 3:30 
QuestionBars getting merged Pin
Pankajk.Singh23-Sep-14 4:15
Pankajk.Singh23-Sep-14 4:15 
I have been doing the same as you are doing, I am using Code 39 for barcode font, its true type. But the problem arises when i am generating smaller in size labels like 1" X 1.5" then bars are getting merged and showing big blocks as together . Can you please tell me where i am making a mistake or any tips that can help me in resolving this issue.
AnswerRe: Bars getting merged Pin
Mohammed Owais30-Sep-14 3:35
professionalMohammed Owais30-Sep-14 3:35 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun21-May-14 19:18
Humayun Kabir Mamun21-May-14 19:18 
GeneralRe: My vote of 5 Pin
Mohammed Owais12-Nov-14 0:54
professionalMohammed Owais12-Nov-14 0:54 
GeneralThank You Pin
sv sathish14-May-14 19:51
sv sathish14-May-14 19:51 
GeneralRe: Thank You Pin
Mohammed Owais12-Nov-14 0:54
professionalMohammed Owais12-Nov-14 0:54 
Questionnice post Pin
Md.Asif Rahman4-Apr-14 13:47
Md.Asif Rahman4-Apr-14 13:47 
AnswerRe: nice post Pin
Mohammed Owais12-Apr-14 2:58
professionalMohammed Owais12-Apr-14 2:58 
GeneralMy vote of 1 Pin
Member 1042568225-Nov-13 16:57
Member 1042568225-Nov-13 16:57 
GeneralRe: My vote of 1 Pin
Mohammed Owais29-Nov-13 23:16
professionalMohammed Owais29-Nov-13 23:16 
QuestionBarcode not showing the correct number or text. Only shows the assinged string name Pin
rajasekar kalidassan19-Nov-13 18:43
rajasekar kalidassan19-Nov-13 18:43 
AnswerRe: Barcode not showing the correct number or text. Only shows the assinged string name Pin
Mohammed Owais21-Nov-13 0:44
professionalMohammed Owais21-Nov-13 0:44 
GeneralRe: Barcode not showing the correct number or text. Only shows the assinged string name Pin
rajasekar kalidassan21-Nov-13 1:58
rajasekar kalidassan21-Nov-13 1:58 
GeneralRe: Barcode not showing the correct number or text. Only shows the assinged string name Pin
Mohammed Owais23-Nov-13 0:56
professionalMohammed Owais23-Nov-13 0:56 
AnswerRe: Barcode not showing the correct number or text. Only shows the assinged string name Pin
Mohammed Owais21-Nov-13 0:50
professionalMohammed Owais21-Nov-13 0:50 
QuestionAbout Barcode Pin
Member 102993939-Nov-13 18:25
Member 102993939-Nov-13 18:25 
AnswerRe: About Barcode Pin
Mohammed Owais11-Nov-13 4:54
professionalMohammed Owais11-Nov-13 4:54 
SuggestionCode 128 Pin
hmdmgd313-Aug-13 14:06
hmdmgd313-Aug-13 14:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.