How to Display BarCodes in Report Header
Problem
In Visual Studio, you cannot use a custom control in headers or footers. Since Aspose.BarCode for Reporting Services is a control this means that you cannot drag and drop the control to the header to display a barcode there. The good news is that you can use images in report headers and footers. This tutorial shows how to use the image control to render barcode images using Aspose.BarCode for Reporting Services.
Solution
- Create a Report Server project in Visual Studio 2005.
- Add a new “Shared Data Source”. In this example, we will use “AdventureWorks” database.
- Add a new blank report to the project.
- Create a new DataSet for the report. Give the following SQL query in the Query String field.
SQL
Select ProductID, Name, ProductNumber From Product
Defining the dataset
- Click OK to save the dataset.
- Add an image control to the report header:
To view the header, switch to layout view.
Select Report, then Page Header.
From the Visual Studio toolbox, drag an Image control to the header.
Select Database as the Image Source.
Setting the image source
Click Next.
From DataSet, select the dataset that you just created for the report.
Select ProductNumber for the Image field. Use the ProductNumber column as the source for the codetext used to generate the barcode.
In MIME type, select image/bmp.
The Image Wizard
- Click Finish to exit the wizard.
- Add a reference to the Aspose.BarCode.ReportingServices.dll:
Select Report, then Report Properties.
Click the References tab.
Browse to the Aspose.BarCode.ReportingServices.dll and add it to the references.
A reference to System.Drawing dll is also required, which can be found in the .NET tab.
On the Classes tab, add the description for the class as follows: Class Name:Aspose.BarCode.ReportingServices.BarCodeBuilderInstance Name: objBarCode
Adding the class description
- Click the Code tab and add the following code:
VB.NET
Public Function GetBarCodeImage(ByVal codetext As String) As Byte()
objBarcode.CodeText = codetext
objBarcode.SymbologyType = Aspose.BarCode.ReportingServices.Symbology.Code39Standard
'Generate the barcode image
Dim ms As New System.IO.MemoryStream
objBarcode.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp)
Return ms.GetBuffer()
End Function
Adding custom code
Click OK to save the reference, class details and the code. So far, we have added the image and the code to generate the barcode.
Click View then DataSets.
Drag the ProductNumber and Name columns to the report body.
Add two textboxes to the body for the purpose of adding labels. The Report layout should look as follows:
Report layout
Right-click the image control and select Properties.
Set the value to =Code.GetBarCodeImage(ReportItems!ProductNumber.Value)
Setting the data value
Save all the files and click on “Preview” tab. You should see the similar output as below:
The final report
If you cannot see the barcode, click the Print Layout icon on the toolbar. This shows the report with the header and footer.