See Also
Other Platforms
Features
Buy Now
Online Help
Demo
Licensing

Back

Using Barcode Professional for Windows Forms in Crystal Reports for .NET

Technologies used
  • Neodynamic Barcode Professional 2.0 (or greater) for Windows Forms (WinControl)
  • Crystal Reports for .NET (any version)
  • Microsoft .NET Framework (any version)
  • Microsoft Visual Studio .NET (any version)
Now, you can put barcode images in your Crystal Reports files using Barcode Professional in a simple manner in your Windows Forms applications.
In the following sample we're going to create a Windows Application which includes a CR report and which shows barcode images into it using Barcode Professional.
For simplicity, we're going to use an XML file as the Data Source for our CR report. This XML file includes information of employees generated from the Microsoft's Northwind database sample.
Follow these steps:
  • Open your .NET development tool � we're going to use Microsoft Visual Studio .NET � and create a new Windows Application.
  • This Guide uses an XML Schema (XSD files) as data source for the report. Please note that this XML Schema will only be used to design the report in the Crystal Reports Designer.
    You can easily generate an XML Schema using an ADO.NET DataSet object. This object has the WriteXmlSchema() method that creates an XML Schema file for you.
    So, if you need to create a report for a Table in a SQL Server Database, you can create a DataSet object for that Table and then call the WriteXmlSchema() method to get the XML Schema file associated to it.
    However, in this sample demo we're going to use an XML file instead of a SQL Server Database for simplicity only.

    REMEMBER that this is required to DESIGN the reports. Once created the report you won't need it at RUNTIME when you deploy your application.

    For sample demos using Typed DataSet or SQL Stored Procedures as Data Sources please refer to the following Guides instead:
  • Add to the project the XML file called Employees.xml (you'll find it at the end of this page ready for downloading)
  • With this file opened, go to the XML menu and click on Create Schema.

    Creating an XML Schema


    VS.NET will create an XML Schema file called Employees.xsd. You'll find it in the Solution Explorer.

    Remember that this file only will be use to design the report.

  • Once we have the XML Schema file, we'll need to modify it and add a NEW element that will represent our barcode images.
    To do this, double-click on the Employees.xsd and add a new element whose name will be BarcodeImage and whose type will be base64Binary as is shown in the following figure:

    Adding a new element to the XML Schema


    After that, save the XML Schema file!
  • Now it's time to create our CR report. So, add a new blank Crystal Reports to the project and name it as AccessCards.rpt.
  • We're going to design our report specifying to it the XML Schema - Employees.xsd. So, with the report opened, in the Field Explorer right-click on Database Fields and go to Add/Remove Database�

    Database Expert


    Click on ADO.NET (XML) data source and in the dialog box write this Employees.XSD in the XML File Path textbox and click on Finish.

    Database Expert


    In the left panel select the Employee item and click on > button as is shown in the above figure. Click OK.
    The Field Explorer should look something like this:

    Field Explorer


  • Design the report as is shown in the following figure:

    BCWinSampleCR


    IMPORTANT: right-click on the BarcodeImage Blob field, select Format and check Can Grow!

    Format Editor


    Save the report and close it.
  • Now, drag and drop a CrystalReportViewer control onto a Windows Form. Set its Dock property to Fill and the DisplayGroupTree to False.
  • Add a reference to the Neodynamic.WinControls.BarcodeProfessional.dll assembly. We're going to use Barcode Professional as a component library.


  • In the code-behind class of the Form, add the following namespace reference:
    Visual Basic .NET
    Imports Neodynamic.WinControls.BarcodeProfessional
    Visual C# .NET
    using Neodynamic.WinControls.BarcodeProfessional;
  • Finally, write the following code in the Form1_Load event procedure:
    Visual Basic .NET
    'Create a DataSet from the XML file
    'REMEMBER to copy the XML file in the same location of the EXE program

    Dim xmlFile As String = System.IO.Directory.GetCurrentDirectory() & "\Employees.xml"
    Dim ds As New DataSet
    ds.ReadXml(xmlFile)
    'The XML File with the data DOES NOT include
    'the BarcodeImage "column" or element we've included
    'in the XML Schema file.
    'So, create the BarcodeImage column
    'WARNING: you must name it as you did in the XML Schema

    Dim dc As DataColumn = New DataColumn("BarcodeImage", GetType(Byte()))
    'Add the BarcodeImage column to the DataTable
    ds.Tables(0).Columns.Add(dc)
    'Create a BarcodeProfessional object to fill
    'the BarcodeImage column

    Dim bc As BarcodeProfessional = New BarcodeProfessional
    'We're going to use Code 128 Barcode Symbology
    bc.Symbology = Symbology.Code128
    bc.Text = ""
    bc.QuietZoneWidth = 0
    bc.ForeColor = Color.FromArgb(0, 116, 232)
    'Now, generate and fill barcode images
    For Each dr As DataRow In ds.Tables(0).Rows
    'We're going to encode the ID column
    bc.Code = CType(dr("ID"), String)
    dr("BarcodeImage") = bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)
    Next
    'Create a AccessCards report object
    'and set its data source with the DataSet

    Dim report As AccessCards = New AccessCards
    report.SetDataSource(ds)
    'Show it!
    CrystalReportViewer1.ReportSource = report
    Visual C# .NET
    //Create a DataSet from the XML file
    //REMEMBER to copy the XML file as the EXE

    string xmlFile = System.IO.Directory.GetCurrentDirectory()+"\\Employees.xml";
    DataSet ds = new DataSet();
    ds.ReadXml(xmlFile);
    //The XML File with the data DOES NOT include
    //the BarcodeImage "column" or element we've included
    //in the XML Schema file.
    //So, create the BarcodeImage column
    //WARNING: you must name it as you did in the XML Schema

    DataColumn dc = new DataColumn("BarcodeImage", typeof(byte[]));
    //Add the BarcodeImage column to the DataTable
    ds.Tables[0].Columns.Add(dc);
    //Create a BarcodeProfessional object to fill
    //the BarcodeImage column

    BarcodeProfessional bc = new BarcodeProfessional();
    //We're going to use Code 128 Barcode Symbology
    bc.Symbology = Symbology.Code128;
    bc.Text = "";
    bc.QuietZoneWidth = 0;
    bc.ForeColor = Color.FromArgb(0,116,232);
    //Now, generate and fill barcode images
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
    //We're going to encode the ID column
    bc.Code = (string)dr["ID"];
    dr["BarcodeImage"] = bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
    }
    //Create a AccessCards report object
    //and set its data source with the DataSet

    AccessCards report = new AccessCards();
    report.SetDataSource(ds);
    //Show it!
    crystalReportViewer1.ReportSource = report;
That's it. Build the Windows Application and run it.
BCWinSampleCR
Download the XML file here:
If you need more information or assistance, please contact our .
 Copyright © 2003 - 2010 Neodynamic S.R.L. All rights reserved.