Crystal Reports barcode generator - C# sample - ByteScout

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()

       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();

           // 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

           // 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"].LogOnInfo.ConnectionInfo.UserID = "test";
           //CrystalReport11.Database.Tables["Table"].LogOnInfo.ConnectionInfo.Password = "test";