How can I Create Barcode to Crystal Report in Visual Studio ?

back to home  >  Tech FAQ  >  Generating Barcode in .NET Crystal Report

Question:

Hi, I need your help!

I use Crystal Report for Visual Studio to create report. Now it's required to add some barcode image to the report. Can you give me some advice? I will appreciate you very much if you can give me some C# examples.

---- Valentine

Answer:

Hi, Valentine.

We make an example for your case to generate barcode image into crystal report. Please see the detail steps as below:

  1. Before programming, we need a Database. Here we used MS Access Data Base. Create a table like this :
  2. Then we need a DataSet binding to Crystal Report Viewer. Make a DataSet like this:
    <?xml version="1.0" standalone="yes"?>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Table">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="ID" type="xs:int" minOccurs="0" />              
                  <xs:element name="ProductName" type="xs:string" minOccurs="0" />
                  <xs:element name="ProductNumber" type="xs:int" minOccurs="0" />
                  <xs:element name="BarcodeImage" type="xs:base64Binary" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    
    Please note: if you create DataSet in VS, the "BarcodeImage" DataType property need to be System.Byte[].
  3. Now start programming. Create a new .NET application as "Crystal Reports Application".
  4. Select Using the Report Wizard, and choose Standard.
  5. In Data dialog, choose "ADO.NET(XML)", and navigate to the DataSet you created in the second step.
  6. Add table to the "Selected Tables".
  7. In CrystalReport1.rpt, drag the three Fields to the report Section 2.
  8. Add the C# code below to the Form1_Load method.
    //create connection to microsoft access data base
    OleDbConnection oleConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Project/DataSet1.mdb");
    oleConn.Open();
    //open product table
    OleDbDataAdapter oleDA = new OleDbDataAdapter("select * from Product", oleConn);
    DataSet ds = new DataSet();
    oleDA.Fill(ds);
                
    //insert a new column to the DataSet to show the barcode image generated
    ds.Tables[0].Columns.Add(new DataColumn("BarcodeImage", typeof(byte[])));
    
    //encode barcode
    Barcode barcode = new Barcode();
    barcode.BarType = BarCodeType.QRCode;
    barcode.Width = 200;
    barcode.Height = 200;
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        //set barcode data to ProductNumber column value
        barcode.Data = (int)dr["ProductNumber"] + "";
        //draw barcode as byte array.
        byte[] imageData = barcode.CreateBarcodeInBytes();
        dr["BarcodeImage"] = imageData;
    }
    
    //bind data to crystal report
    CrystalReport1 rpt = new CrystalReport1();
    rpt.SetDataSource(ds);
    this.crystalReportViewer1.ReportSource = rpt;
    oleConn.Close();
    
  9. Download "PQScan.BarcodeCreator.dll" and add reference to project.
  10. Run the project, you will get a Crystal Report with barcode in.

Hope the resolution for the generating barcode in .NET Crystal Report can be helpful.

---- pqScan Support Team

Online Demo Download Trial How to Encode QR Code with Chinese Words in C# ? How to Insert Barcode Control to MS Excel WorkSheetin C# ? How to Add Barcode Image to MS Excel in C# ? Can I Read Barcode in PDF document using C#? Can I Get a Compression Jpeg Image from PDF ? Merge A Group of Multi-page Tiff to PDF documentusing C#? Translate JPG Image to PDF document in C#? Convert specific pages from PDF to images in C# Is it available for reading all of the barcode in a picture by one time? Do You Support Translate Images to PDF from SQL Database? Question about Image Size.