Click here to Skip to main content
15,867,568 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.1K   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

 
QuestionNOT ABLE TO READ BARCODE Pin
Member 1457209212-Nov-19 1:16
Member 1457209212-Nov-19 1:16 
QuestionAbout Input Form Pin
Member 140768015-Jan-19 22:34
Member 140768015-Jan-19 22:34 
QuestionCode 39 Styles Pin
Member 1304307917-Jun-18 22:45
Member 1304307917-Jun-18 22:45 
QuestionObject Does not contain a definition for Database and no extension method Database Pin
Member 135774263-Jan-18 18:50
Member 135774263-Jan-18 18:50 
AnswerRe: Object Does not contain a definition for Database and no extension method Database Pin
Mohammed Owais4-Jan-18 2:15
professionalMohammed Owais4-Jan-18 2:15 
Questionhow to print barcode labels with Micrsofte reports viewer Pin
Member 1307013619-Mar-17 19:22
Member 1307013619-Mar-17 19:22 
AnswerRe: how to print barcode labels with Micrsofte reports viewer Pin
Mohammed Owais30-Mar-17 7:05
professionalMohammed Owais30-Mar-17 7:05 
QuestionFlashControlV71.dll Pin
Member 106105754-Sep-16 23:25
Member 106105754-Sep-16 23:25 
AnswerRe: FlashControlV71.dll Pin
Mohammed Owais30-Mar-17 7:03
professionalMohammed Owais30-Mar-17 7:03 
QuestionChange Detail section width and page setting from c# code Pin
shethgokulesh13-Apr-16 1:45
shethgokulesh13-Apr-16 1:45 
AnswerRe: Change Detail section width and page setting from c# code Pin
Mohammed Owais22-Apr-16 1:49
professionalMohammed Owais22-Apr-16 1:49 
QuestionCustom lables Pin
Ahmad Negm18-Feb-16 3:51
Ahmad Negm18-Feb-16 3:51 
AnswerRe: Custom lables Pin
Mohammed Owais26-Feb-16 1:26
professionalMohammed Owais26-Feb-16 1:26 
QuestionAsking for database login Pin
Member 1200386728-Oct-15 0:16
Member 1200386728-Oct-15 0:16 
AnswerRe: Asking for database login Pin
Mohammed Owais5-Nov-15 0:48
professionalMohammed Owais5-Nov-15 0:48 
GeneralRe: Asking for database login Pin
Member 120038675-Nov-15 17:50
Member 120038675-Nov-15 17:50 
GeneralRe: Asking for database login Pin
Mohammed Owais7-Nov-15 17:37
professionalMohammed Owais7-Nov-15 17:37 
Questioncomment Pin
Member 1160963415-Apr-15 3:35
Member 1160963415-Apr-15 3:35 
GeneralRe: comment Pin
Mohammed Owais17-Apr-15 21:31
professionalMohammed Owais17-Apr-15 21:31 
QuestionBarcode Integration in ASP.Net Pin
r.brahma3-Dec-14 0:21
r.brahma3-Dec-14 0:21 
AnswerRe: Barcode Integration in ASP.Net Pin
Mohammed Owais3-Dec-14 2:46
professionalMohammed Owais3-Dec-14 2:46 
QuestionVS 2013 support Pin
Member 416585113-Nov-14 2:52
Member 416585113-Nov-14 2:52 
AnswerRe: VS 2013 support Pin
Mohammed Owais14-Nov-14 8:02
professionalMohammed Owais14-Nov-14 8:02 
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 

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.