CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2008
    Posts
    136

    Thumbs up How to create Crystal Reports featuring barcode images using Typed DataSet in ASP.NET

    Prerequisites
    - Neodynamic Barcode Professional 3.0 (or greater) for ASP.NET (WebControl)
    - Microsoft .NET Framework 2.0 (or greater)
    - Microsoft Visual Studio 2005
    - Microsoft SQL Server 2005 (any version) with Northwind Database sample installed
    - Crystal Report for Visual Studio .NET 2005

    In the following Step-By-Step Guide we're going to create a Crystal Report which features barcoding capabilities by using Barcode Professional for ASP.NET and using as data source for the report a Typed DataSet.

    Follow these steps:
    - Open Visual Studio and create a new ASP.NET Website naming it CrystalReportBarcodeDataSet.
    - Add a new DataSet item to the project and name it Northwind.xsd

    View Figure...
    Adding a Typed DataSet

    Click Add button. You will be asked if you want to place the Northwind.xsd file in the App_Code directory. Answer yes.

    After that, the TableAdapter Configuration Wizard is automatically launched so please follow its steps.

    In the first step, please create a connection to the Northwind SQL Server Database sample and click Next. In the second step, choose "Use SQL statements" and click Next. After that, please enter the following SQL Statement:

    SELECT ProductID, ProductName, UnitPrice FROM Products

    View Figure...
    Specifying a SQL Statement that returns Northwind Product info

    Click Finish to close the Wizard dialog box.

    - After that, add a new custom Column to the DataTable just created and name it Barcode as is shown in the following figure

    View Figure...
    Adding a new Column to the DataTable for barcoding purpose

    - Change the data type of the Barcode column to System.Byte[] (Array of Byte). NOTE: the System.Byte[] data type is not listed and thus why you must type it manually.

    View Figure...
    Setting up the Barcode Column to System.Byte[] data type

    - Save the Northwind.xsd file.
    - Now add a new Crystal Report item to the project and name it CrystalReportBarcode.rpt.

    View Figure...
    Adding a Crystal Report to the project

    Click Add button. Next, choose "Blank Report" in the Crystal Report Gallery dialog box.

    View Figure...
    Creating a blank report

    Click OK button.

    - Launch Crystal Report Database Expert by going to Crystal Reports > Database > Database Expert...

    View Figure...
    Opening Crystal Reports Database Expert dialog box

    In the Database Expert dialog box select the Products DataTable (from the left pane) so it appears in the "Selected Tables" list (right pane) as is shown in the following figure.

    View Figure...
    Choosing the Products DataTable as data source of the Crystal Report

    Click OK button. After that, the Products DataTable should be available in the Field Explorer Window as is shown in the following figure. NOTE: To display the Field Explorer press Ctrl+Alt+T

    View Figure...
    The Products DataTable available in the Field Explorer

    - Please design the report so it looks like the following figure. Just drag & drop the fields from the Field Explorer onto the report as is shown in the figure.

    View Figure...
    The barcode report layout

    The most important field in our scenario is Barcode (Please DO NOT change the dimensions of such field on the report surface). Select the Barcode item on the report and right-clicking onto it select "Format Object" from the context menu to open the Format Editor dialog box. In that dialog box, please check Can Grow option and click OK button.

    View Figure...
    Setting up "Can Grow" option for the Barcode item in the Format Editor dialog box

    - Save the report.
    - Now Create/Open an ASP.NET WebForm at design time and drag & drop a CrystalReportViewer control onto it.
    - After that, from the Solution Explorer, add a reference to Barcode Professional for ASP.NET assembly: Neodynamic.WebControls.Barcodeprofessional.dll
    - Write the following code in the Page_Load event procedure.

    Visual Basic .NET
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Filling Products DataTable from DB
    Dim ta As New NorthwindTableAdapters.ProductsTableAdapter()
    Dim dt As New Northwind.ProductsDataTable()
    ta.Fill(dt)

    'Create an instance of Barcode Professional
    Dim bcp As New Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional()
    'Barcode settings
    bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code39
    bcp.Extended = true
    bcp.AddChecksum = false
    bcp.BarHeight = 0.4f

    'Append fictitious Company internal prefix
    Dim prodPrefix As String = "12345"

    Dim row As Northwind.ProductsRow
    For Each row In dt.Rows
    'Set the value to encode
    bcp.Code = prodPrefix + row.ProductID.ToString()
    'Generate the barcode image and store it into the Barcode Column
    row.Barcode = bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)
    Next

    'Create a AveryMailLabels report object
    'and set its data source with the DataSet
    Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
    Dim rptFile As String = Server.MapPath("CrystalReportBarcode.rpt")
    report.Load(rptFile)
    report.SetDataSource(CType(dt, DataTable))

    Me.CrystalReportViewer1.ReportSource = report
    End Sub


    Visual C# .NET
    protected void Page_Load(object sender, EventArgs e)
    {
    //Filling Products DataTable from DB
    NorthwindTableAdapters.ProductsTableAdapter ta = new NorthwindTableAdapters.ProductsTableAdapter();
    Northwind.ProductsDataTable dt = new Northwind.ProductsDataTable();
    ta.Fill(dt);

    //Create an instance of Barcode Professional
    Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional bcp = new Neodynamic.WebControls.BarcodeProfessional.BarcodeProfessional();
    //Barcode settings
    bcp.Symbology = Neodynamic.WebControls.BarcodeProfessional.Symbology.Code39;
    bcp.Extended = true;
    bcp.AddChecksum = false;
    bcp.BarHeight = 0.4f;

    //Append fictitious Company internal prefix
    string prodPrefix = "12345";

    //Update DataTable with barcode image
    foreach (Northwind.ProductsRow row in dt.Rows)
    {
    //Set the value to encode
    bcp.Code = prodPrefix + row.ProductID.ToString();
    //Generate the barcode image and store it into the Barcode Column
    row.Barcode = bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png);
    }

    //Create a report object
    //and set its data source with the DataSet
    CrystalDecisions.CrystalReports.Engine.ReportDocument report;
    report = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
    string rptFile = Server.MapPath("CrystalReportBarcode.rpt");
    report.Load(rptFile);
    report.SetDataSource((DataTable)dt);

    this.CrystalReportViewer1.ReportSource = report;
    }

    - That's it. Run your application. You should get the barcode images displayed on the report.

    View Figure...
    The Crystal Report featuring barcodes generated by Barcode Professional

    CrystalReportViewer control lets you to export the displayed report to Acrobat PDF, Microsoft Excel XLS as well as other well know document formats. In all cases the barcode images are maintained.

    View Figure...
    The Crystal Repor report in Acrobat PDF format featuring barcodes generated by Barcode Professional

    Links:
    This Demo
    More Demos
    Download Barcode Professional for ASP.NET
    More Information about Neodynamic Barcode Professional for ASP.NET


    Neodynamic
    .NET Components & Controls
    http://www.neodynamic.com
    http://www.barcode-for-aspnet.com

  2. #2
    Join Date
    Nov 2014
    Posts
    3

    Re: How to create Crystal Reports featuring barcode images using Typed DataSet in ASP

    Excellent post,i learn a lot from it,thanks.I am just new to ASP.NET barcode generation filed and recently i met some problems in printing barcodes using crystal reports in ASP.NET.I used code 128 to a generate bar code in crystal report , so when i print report in a A4 it detect well but when it print in sticker those are not detect by bar code detector but i checked using bar code printing software it works fine.Can i use Typed DataSet in ASP.NET to do this,is it possible?Some details will be appreciated.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured