Using Barcode Professional for ASP.NET in Crystal Reports for .NET - PDF Support
Product Barcode Professional for ASP.NET Published 01/12/2007 Updated 01/12/2007
Overview
Now, you can deliver barcode images in your Crystal Reports web reports using Barcode Professional. Following this FAQ you can export your report to PDF format maintaining the barcode images into it!
Requirements
Neodynamic Barcode Professional 2.0 (or greater) for ASP.NET (WebControl)
Crystal Reports for .NET (any version)
Microsoft .NET Framework (any version)
Microsoft Visual Studio .NET (any version)
Microsoft SQL Server Northwind Database
In the following sample we're going to create an ASP.NET Web Application which includes a CR reports and which shows barcode images into that report using Barcode Professional.
In this sample, we're going to use the Microsoft's Northwind database shipped with SQL Server. If you don't have SQL Server, you could use MSDE and get the Northwind database sample from Microsoft Office CD too.
Follow these steps:
- Open your .NET development tool such as Visual Studio .NET and create a new ASP.NET Web Application.
IMPORTANT NOTE:
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.
REMEMBER that this is required to DESIGN the reports. Once created the report you won#&39;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: - Creating an XML Schema for our Report's DataSource. In this case we're going to create a simple SQL SELECT statement of the Northwind's Order Table. Please follow these steps:
- Add a new "temporary" Webform called SchemaGen.aspx
- In the Page_Load event procedure write the following code:
VB
'Connection Info 'CHANGE the values as you need Dim scnn As String = "server=(local);database=NorthwindCS;trusted_connection=true" Dim cnn As New SqlClient.SqlConnection(scnn) 'Get Orders info Dim sqlOrders As String = "SELECT dbo.Orders.OrderID, dbo.Customers.CompanyName, dbo.Customers.Address, dbo.Customers.City, dbo.Customers.PostalCode, dbo.Customers.Country, dbo.Orders.OrderDate, dbo.Orders.RequiredDate, dbo.Orders.ShippedDate, dbo.Orders.ShipVia, dbo.Orders.Freight, dbo.Products.ProductID, dbo.Products.ProductName, dbo.[Order Details].UnitPrice, dbo.[Order Details].Quantity, dbo.[Order Details].UnitPrice * dbo.[Order Details].Quantity AS TotalLine, dbo.Customers.Region FROM dbo.Customers INNER JOIN dbo.Orders ON dbo.Customers.CustomerID = dbo.Orders.CustomerID INNER JOIN dbo.[Order Details] ON dbo.Orders.OrderID = dbo.[Order Details].OrderID INNER JOIN dbo.Products ON dbo.[Order Details].ProductID = dbo.Products.ProductID" Dim cmd As New SqlClient.SqlCommand(sqlOrders, cnn) 'Create DataSet for Orders Dim da As New SqlClient.SqlDataAdapter(cmd) Dim ds As New DataSet da.Fill(ds) 'Create the XML Schema for Orders 'Be sure you give write permission to ASPNET account to Temp folder ds.WriteXmlSchema("C:\temp\orders.xsd")C#
//Connection Info //CHANGE the values as you need string scnn = "server=(local);database=NorthwindCS;trusted_connection=true"; System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(scnn); //Get Orders info string sqlOrders = "SELECT dbo.Orders.OrderID, dbo.Customers.CompanyName, dbo.Customers.Address, dbo.Customers.City, dbo.Customers.PostalCode, dbo.Customers.Country, dbo.Orders.OrderDate, dbo.Orders.RequiredDate, dbo.Orders.ShippedDate, dbo.Orders.ShipVia, dbo.Orders.Freight, dbo.Products.ProductID, dbo.Products.ProductName, dbo.[Order Details].UnitPrice, dbo.[Order Details].Quantity, dbo.[Order Details].UnitPrice * dbo.[Order Details].Quantity AS TotalLine, dbo.Customers.Region FROM dbo.Customers INNER JOIN dbo.Orders ON dbo.Customers.CustomerID = dbo.Orders.CustomerID INNER JOIN dbo.[Order Details] ON dbo.Orders.OrderID = dbo.[Order Details].OrderID INNER JOIN dbo.Products ON dbo.[Order Details].ProductID = dbo.Products.ProductID"; System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlOrders, cnn); //Create DataSet for Orders System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); //Create the XML Schema for Orders //Be sure you give write permission to ASPNET account to Temp folder ds.WriteXmlSchema("C:\\temp\\orders.xsd"); - Set the SchemaGen.aspx page as the start page
- Run the app. It should create the Orders.XSD file in the C:\Temp folder
Now, we have the XML Schema needed to DESIGN the report. - Add the created XML Schema called Orders.XSD to the ASP.NET Web App Project.
- Once we have the XML Schema file, we'll need to modify it and add a NEW element that will represent our barcode image.
To do this, on the Solution Explorer, double-click on the Orders.xsd and add a new element whose name will be BarcodeImage and whose type will be base64Binary as is shown in the following figure:
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 Orders.rpt
- We're going to design our report specifying to it the XML Schema - Orders.xsd. So, with the report opened, in the Field Explorer right-click on Database Fields and go to Add/Remove Database
Click on ADO.NET (XML) data source and in the dialog box write Orders.XSD in the XML File Path textbox and click on Finish
In the left panel select the Table item and click on > button as is shown in the above figure. Click OK.
- The Field Explorer should look something like this:
- Create a Group in the report for the OrderID field.
- Design the Order report as is shown in the next figure:
IMPORTANT: right-click on the BarcodeImage Blob field, select Format and check Can Grow!
Save the report and close it.
- Now, open the WebForm1.aspx of your project and drag & drop a CrystalReportViewer control onto it. Set the DisplayGroupTree property to False.
- Add a reference to the Neodynamic.WebControls.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:
VB
Imports Neodynamic.WebControls.BarcodeProfessionalC#
using Neodynamic.WenControls.BarcodeProfessional; - Finally, write the following code in the Page_Load event procedure:
VB
'Connection Info 'CHANGE the values as you need Dim scnn As String = "server=(local);database=NorthwindCS;trusted_connection=true" Dim cnn As New SqlClient.SqlConnection(scnn) 'Get Orders info 'Only Country = USA Dim sqlOrders As String = "SELECT dbo.Orders.OrderID, dbo.Customers.CompanyName, dbo.Customers.Address, dbo.Customers.City, dbo.Customers.PostalCode, dbo.Customers.Country, dbo.Orders.OrderDate, dbo.Orders.RequiredDate, dbo.Orders.ShippedDate, dbo.Orders.ShipVia, dbo.Orders.Freight, dbo.Products.ProductID, dbo.Products.ProductName, dbo.[Order Details].UnitPrice, dbo.[Order Details].Quantity, dbo.[Order Details].UnitPrice * dbo.[Order Details].Quantity AS TotalLine, dbo.Customers.Region FROM dbo.Customers INNER JOIN dbo.Orders ON dbo.Customers.CustomerID = dbo.Orders.CustomerID INNER JOIN dbo.[Order Details] ON dbo.Orders.OrderID = dbo.[Order Details].OrderID INNER JOIN dbo.Products ON dbo.[Order Details].ProductID = dbo.Products.ProductID WHERE dbo.Customers.Country = 'USA'" Dim cmd As New SqlClient.SqlCommand(sqlOrders, cnn) 'Create DataSet for Orders Dim da As New SqlClient.SqlDataAdapter(cmd) Dim ds As New DataSet da.Fill(ds) 'The DataSet 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 are going to use Code 128 Barcode Symbology 'but you could use other bc.Symbology = Symbology.Code128 'Set some properties of Barcode Professional. Play with them. bc.BarHeight = 0.5F 'Set System.Web.UI.WebControls.Unit.Pixel(50) if you use Barcode Professional 2.X bc.Text = "" bc.QuietZoneWidth = 0 'Set System.Web.UI.WebControls.Unit.Pixel(0) if you use Barcode Professional 2.X bc.DisplayCode = False 'Now, generate and fill barcode images For Each dr As DataRow In ds.Tables(0).Rows 'We're going to encode the OrderID column bc.Code = CStr(dr("OrderID")) dr("BarcodeImage") = bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png) Next 'Create a Orders report object 'and set its data source with the DataSet Dim report As New Orders report.SetDataSource(ds) 'Export report to PDF format 'NOTE: Give write permission to ASPNET account to Temp folder Dim DiskOptions As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions DiskOptions.DiskFileName = "C:\TEMP\OrdersWithBarcodeImages.pdf" report.ExportOptions.DestinationOptions = DiskOptions report.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile report.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat report.Export() 'And Show it! CrystalReportViewer1.ReportSource = reportC#
//Connection Info //CHANGE the values as you need string scnn = "server=(local);database=NorthwindCS;trusted_connection=true"; System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(scnn); //Get Orders info //Only Country = USA string sqlOrders = "SELECT dbo.Orders.OrderID, dbo.Customers.CompanyName, dbo.Customers.Address, dbo.Customers.City, dbo.Customers.PostalCode, dbo.Customers.Country, dbo.Orders.OrderDate, dbo.Orders.RequiredDate, dbo.Orders.ShippedDate, dbo.Orders.ShipVia, dbo.Orders.Freight, dbo.Products.ProductID, dbo.Products.ProductName, dbo.[Order Details].UnitPrice, dbo.[Order Details].Quantity, dbo.[Order Details].UnitPrice * dbo.[Order Details].Quantity AS TotalLine, dbo.Customers.Region FROM dbo.Customers INNER JOIN dbo.Orders ON dbo.Customers.CustomerID = dbo.Orders.CustomerID INNER JOIN dbo.[Order Details] ON dbo.Orders.OrderID = dbo.[Order Details].OrderID INNER JOIN dbo.Products ON dbo.[Order Details].ProductID = dbo.Products.ProductID WHERE dbo.Customers.Country = 'USA'"; System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlOrders, cnn); //Create DataSet for Orders System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); //The DataSet 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 System.Data.DataColumn dc = new System.Data.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 are going to use Code 128 Barcode Symbology //but you could use other bc.Symbology = Symbology.Code128; //Set some properties of Barcode Professional. Play with them. bc.BarHeight = 0.5f; //Set System.Web.UI.WebControls.Unit.Pixel(50) if you use Barcode Professional 2.X bc.Text = ""; bc.QuietZoneWidth = 0; //Set System.Web.UI.WebControls.Unit.Pixel(0) if you use Barcode Professional 2.X bc.DisplayCode = false; //Now, generate and fill barcode images foreach(System.Data.DataRow dr in ds.Tables[0].Rows) { //We are going to encode the OrderID column bc.Code = dr["OrderID"].ToString(); dr["BarcodeImage"] = bc.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png); } //Create a Orders report object //and set its data source with the DataSet Orders report = new Orders(); report.SetDataSource(ds); //Export report to PDF format //NOTE: Give write permission to ASPNET account to Temp folder CrystalDecisions.Shared.DiskFileDestinationOptions DiskOptions = new CrystalDecisions.Shared.DiskFileDestinationOptions(); DiskOptions.DiskFileName = "C:\\TEMP\\OrdersWithBarcodeImages.pdf"; report.ExportOptions.DestinationOptions = DiskOptions; report.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile; report.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat; report.Export(); //And Show it! CrystalReportViewer1.ReportSource = report; - Set the WebForm1.aspx page as the start page
That's it. Build the ASP.NET Web Application and run it. You'll see the following output.
And you can find the exported PDF file under C:\Temp\OrdersWithBarcodeImages.pdf
Sample Files Download
Here are a VB.NET and C# versions of this sample. Please, download the appropriate zip file. In it you'll find these files:
- WebForm1.aspx + WebForm1.aspx.cs/vb
- SchemaGen.aspx + SchemaGen.aspx.cs/vb
- Orders.XSD
- Orders.rpt
To reproduce this sample on your own machine, create a new ASP.NET Web Application. Name it as BCWebSampleCRPDF, add to it the downloaded files and review the steps described above.
Download the source code here:
- For Visual Basic.NET: BCWebSampleCrystalReportsPDFVB.zip (11 KB)
- For Visual C#.NET: BCWebSampleCrystalReportsPDFCS.zip (11 KB)
Need help? Contact Support
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.