Crystal Reports barcode generator - C# sample - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

Crystal Reports barcode generator – C# sample

  • Home
  • /
  • Articles
  • /
  • Crystal Reports barcode generator – C# sample

Crystal Reports barcode generation tutorial shows how to create barcodes in Crystal Reports using C#. Use C Sharp source code sample below to generate bar code.

BarCode Generator SDK supports Crystal Report barcode generation. Full source code for this tutorial is included with evaluation version of the SDK in Examples > Crystal Reports folder.

C# (Form1.cs):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using Bytescout.BarCode;

namespace CrystalReportsWithBarcode
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void crystalReportViewer1_Load(object sender, EventArgs e)
       {
           // create (duplicate) connection for report fields
           // we use OleDbConnection to the sample Access database
           // you should create your connection yourselves

           string sDatabaseFile = Directory.GetCurrentDirectory() + "\products.mdb";
           OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sDatabaseFile);

           // use SQL query you previously remembered
           OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT `Products`.`ProductName`, `Products`.`ProductDescription` FROM   `Products` `Products`", aConnection);

           // fill dataset
           DataSet ds = new DataSet();
           dataAdapter.Fill(ds);

           // add virtual field that will be used for generated barcode data
           ds.Tables[0].Columns.Add(new DataColumn("BarcodeImage", typeof(byte[])));

           // don't forget to close the connection
           aConnection.Close();

           // create barcode object
           Barcode bc = new Barcode(SymbologyType.QRCode);
           bc.DrawCaption = false;
           //bc.NarrowBarWidth = 1;
           //bc.WideToNarrowRatio = 2;


           foreach (DataRow dr in ds.Tables[0].Rows)
           {
               // set barcode object's Value property to a value of a field
               // you want to be used for barcode creation
               // we use 5 first symbols of product name
               bc.Value = (dr["ProductName"] as string).Substring(0, 5);

               // retrieve generated image bytes
               byte[] barcodeBytes = bc.GetImageBytesWMF();

               // fill virtual field with generated image bytes
               dr["BarcodeImage"] = barcodeBytes;
           }

           // attach data source to virtual table
           CrystalReport11.Database.Tables["Table"].SetDataSource(ds);

           //CrystalReport11.Database.Tables["Table"].LogOnInfo.ConnectionInfo.UserID = "test";
           //CrystalReport11.Database.Tables["Table"].LogOnInfo.ConnectionInfo.Password = "test";

       }
   }
}

Tutorials:

prev
next