SSRS Barcode Solution using Windows Forms Control | WinForms

SSRS Barcode Forms RepresentationThis SSRS .NET Barcode Windows® Forms Control tutorial provides steps for generating barcodes in Microsoft SQL Server Reporting Services and Visual Studio .NET environments.

Compatibility:
  • Visual Studio .NET 2003, 2005, 2008, 2010, and 2012
  • SQL Server Reporting Services (SSRS) 2000, 2005, 2008, 2008R2, 2012 and 2014
  • Microsoft .NET Framework 

Product Update Notice: The SSRS Native Barcode Generator is now available and offers an easier method of generating barcodes without fonts or other components. IDAutomation now recommends using this product instead of the forms control.

SSRS Barcode Tutorial Overview

A Reporting Services Image Control or Text Box Control can be linked to the IDAutomation .NET Barcode Windows Forms Control to create a valid image of a barcode for display in a report.

  • This tutorial will show how to include the IDAutomation .NET Windows Forms Control in a project and how to create a report using the control.
  • This example will show how to build a report based on the 'Customers' table of the AdventureWorks database.
  • The report will include the database fields of Customer_ID, Store_ID, and Account_Number.
  • The BackgroundImage property of a text box control in the report will be added and set to represent the barcode.

Once the report is created, the security settings for the deployment environment must be configured to allow custom assemblies to run.

SSRS Barcode Windows Forms Control Tutorial Applications & Components
.NET Forms Control Class Name
IDAutomation.LinearBarCode.dll IDAutomation.Windows.Forms.LinearBarCode.Barcode
IDAutomation.Databar.dll IDAutomation.Windows.Forms.Databar_Barcode.Databar
IDAutomation.Aztec.dll IDAutomation.Windows.Forms.AztecBarcode.AztecBarcode
IDAutomation.DataMatrix.dll IDAutomation.Windows.Forms.DataMatrixBarcode.DataMatrixBarcode
IDAutomation.Maxicode.dll IDAutomation.Windows.Forms.Maxicode.Maxicode
IDAutomation.PDF417.dll IDAutomation.Windows.Forms.PDF417Barcode.PDF417Barcode
IDAutomation.QRCode.dll IDAutomation.Windows.Forms.QRCodeBarcode.QRCodeBarcode

Reporting Services SSRS Barcode Tutorial

Set Up & Installation
  1. Download the purchased or demo of the IDAutomation .NET Barcode Windows Forms Control. This example uses the IDAutomation.LinearBarCode.dll to generate Code 128 barcodes.
  2. Copy the IDAutomation.LinearBarcode.dll to the following directories:
    %ProgramFiles%\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies
    %Program Files%\Microsoft SQL Server\MSRS11.MSSQLSERVER\Reporting Services\ReportServer\bin
    NOTE: For assistance on how to configure this solution for your environment, review Microsoft's Deploying a Custom Assembly article.
  3. Navigate to
    %ProgramFiles%\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies
    and open the RSPreviewPolicy configuration file. The RSPreviewPolicy sets Visual Studio to display the barcode during a Preview. If FullTrust is not granted to the code groups in this file, then the error message "That assembly does not allow partially trusted callers" will appear during the preview. Change the PermissionSetName from Execution to FullTrust.
    In the RSPreviewPolicy.config file, place this code after the first CodeGroup:
    <CodeGroupclass="FirstMatchCodeGroup"version="1" PermissionSetName="FullTrust" Name="IDAutomationLinearBarCode"Description="This code group grants IDAutomation.LinearBarCode.dll Full Trust permission and allow Preview in Visual Studio.">
    <IMembershipConditionclass="UrlMembershipCondition" version="1"Url="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies\IDAutomation.LinearBarCode.dll"/> </CodeGroup>
  4. Navigate to
    %ProgramFiles%\Microsoft SQL Server\MSRS11.MSSQLSERVER\Reporting Services\ReportServer
    and open the rssrvpolicy configuration file.
    In the rssrvpolicy.config file, place this code after the first CodeGroup:
    <CodeGroup class="UnionCodeGroup" version="1" PermissionSetName="FullTrust" Description="This code group grants IDAutomationLinearBarCode.dll Full Trust permission.">
    <IMembershipCondition class="UrlMembershipCondition"version="1" Url="C:\Program Files\Microsoft SQL Server\MSRS11.MSSQLSERVER\Reporting Services\ReportServer\bin\IDAutomation.LinearBarCode.dll" /> </CodeGroup>
  5. Reboot the server or servers that are running the Visual Studio and SQL Server Reporting Services applications.
Tutorial
  1. Run Visual Studio and open the report.
  2. To activate the Report menu in the VS .NET IDE, left-click on the body of the report. Select Report - Report Properties.
    Activate the Report Menu.
  3. Select References. In the Add or Remove assemblies, select Add and then select the ellipsis button ( ellipsis ).
  4. Select the Browse tab, choose the IDAutomation.LinearBarCode.dll, and select OK.
  5. In order to use the IDAutomation Windows Forms Control in a report, two additional assemblies need to be included in the project. Select Add, choose the .NET tab and load System.Windows.Forms and System.Drawing.
  6. In the Add or Remove classes, set the Class Name. For the IDAutomation.LinearBarCode.dll, use IDAutomation.Windows.Forms.LinearBarCode.Barcode.
  7. Set an instance name. This tutorial uses objBarcode.
    Set References to Assemblies and Classes in the SSRS Report.
  8. The most common use of the control is to use the BMPPicture property to create an image that can be included in the report. Select the Code tab on the left and insert this code, which returns a property representing a byte stream of the image: 
    Public Function GetLinear(Code As String) as Byte()
     dim stream as System.IO.MemoryStream = New System.IO.MemoryStream
     dim bitmapBytes as Byte()
     objBarcode.SymbologyID = IDAutomation.Windows.Forms.LinearBarCode.Symbologies.Code128
     objBarcode.Resolution = 2
     objBarcode.ResolutionCustomDPI = 96
     objBarcode.DataToEncode = Code
     objBarcode.XDimensionMILS = 11
     objBarcode.NarrowToWideRatio = 3
     objBarcode.CheckCharacter = false
     objBarcode.BarHeightCM = 2.54
     objBarcode.ShowText = false
     objBarcode.ApplyTilde = true
     objBarcode.BMPPicture.Save(stream, System.Drawing.Imaging.ImageFormat.jpeg)
     bitmapBytes = stream.ToArray
     stream.Close()
     Return bitmapBytes
    End Function
  9. To add a field to the report, right-click the last column in the table and select Insert Column - Right. Name the field "Barcode."
    Insert a Column for the Barcode Field in the SSRS Report.
  10. To display the image of a barcode representing the Customer ID value, use the GetLinear () function created in Step 8. This is done by setting the values in the BackgroundImage property of the text box in the Barcode column. Left-click the Barcode field's text box to access the Properties Window. If it does not display, select View - Properties Window. Expand the BackgroundImage property and set:
    • Source - Select Database from the list. This signifies that the value for the barcode will be pulled from a database field.
    • MIMEType - Select image/jpeg from the list. This signifies the format of the image.
    • BackgroundRepeat - Select Clip or NoRepeat from the list. This ensures that only one image is placed in the text box.
    • Value - =code.GetLinear(Fields!CustomerID.Value). Value sets the report to use the function created in the code module of the report properties. The function uses the chosen field from the database table to create the barcode.
      Enter the formula in the SSRS Report.
  11. To ensure the barcode is returned, select the Preview tab.
    View Barcode in Preview Tab
  12. Before deploying the report to the Web server, the user may have to update the security settings of the .NET Framework to give the IDAutomation.LinearBarCode.dll assembly permission to run. Microsoft has documented these configuration steps in Knowledge Base article 842419.
  13. To deploy the solution to the Report Server, select Build - Deploy Solution. For guidance on how to set up deployment, view Microsoft's How to Set Deployment Properties article.

DataBar and 2D Examples

Step 8 explains how to create a function that will render the barcode for the IDAutomation.LinearBarCode.dll. The examples below are recommended for DataBar and 2D barcode symbologies. For a complete list of properties, view the Properties list in the Forms Control user guide.

DataBar:

Public Function GetDatabar(Code As String) as Byte()
dim stream as System.IO.MemoryStream = New System.IO.MemoryStream
dim bitmapBytes as Byte()
objDatabar.Resolution = 2
objDatabar.ResolutionCustomDPI = 96
objDatabar.SymbologyID = IDAutomation.Windows.Forms.Databar_Barcode.Databar.Symbologies.Databar
objDatabar.DataToEncode = Code
objDatabar.ApplyTilde = true
objDatabar.BMPPicture.Save(stream, System.Drawing.Imaging.ImageFormat.jpeg)
bitmapBytes = stream.ToArray
stream.Close()
Return bitmapBytes
End Function

Aztec:

Public Function GetAztec(Code As String) as Byte()
dim stream as System.IO.MemoryStream = New System.IO.MemoryStream
dim bitmapBytes as Byte()
objAztec.Resolution = 2
objAztec.ResolutionCustomDPI = 96
objAztec.EncodingMode = IDAutomation.Windows.Forms.AztecBarcode.EncodingModes.Auto
objAztec.DataToEncode = Code
objAztec.XDimensionMILS = 24
objAztec.ApplyTilde = true
objAztec.BMPPicture.Save(stream, System.Drawing.Imaging.ImageFormat.jpeg)
bitmapBytes = stream.ToArray
stream.Close()
Return bitmapBytes
End Function

Data Matrix:

Public Function GetDataMatrix(Code As String) as Byte()
dim stream as System.IO.MemoryStream = New System.IO.MemoryStream
dim bitmapBytes as Byte()
objDataMatrix.Resolution = 2
objDataMatrix.ResolutionCustomDPI = 96
objDataMatrix.EncodingMode = IDAutomation.Windows.Forms.DataMatrixBarcode.EncodingModes.Auto
objDataMatrix.PreferredFormat = IDAutomation.Windows.Forms.DataMatrixBarcode.PreferredFormats.Auto
objDataMatrix.DataToEncode = Code
objDataMatrix.XDimensionMILS = 24
objDataMatrix.ApplyTilde = true
objDataMatrix.BMPPicture.Save(stream, System.Drawing.Imaging.ImageFormat.jpeg)
bitmapBytes = stream.ToArray
stream.Close()
Return bitmapBytes
End Function

MaxiCode:

Public Function GetMaxicode(Code As String) as Byte()
dim stream as System.IO.MemoryStream = New System.IO.MemoryStream
dim bitmapBytes as Byte()
objMaxicode.Resolution = 2
objMaxicode.ResolutionCustomDPI = 203
objMaxicode.EncodingMode = IDAutomation.Windows.Forms.Maxicode.EncodingModes.Mode2
objMaxicode.DataToEncode = Code
objMaxicode.ApplyTilde = true
objMaxicode.BMPPicture.Save(stream, System.Drawing.Imaging.ImageFormat.jpeg)
bitmapBytes = stream.ToArray
stream.Close()
Return bitmapBytes
End Function

PDF417:

Public Function GetPDF417(Code As String) as Byte()
dim stream as System.IO.MemoryStream = New System.IO.MemoryStream
dim bitmapBytes as Byte()
objPDF417.Resolution = 2
objPDF417.ResolutionCustomDPI = 96
objPDF417.PDFMode = IDAutomation.Windows.Forms.PDF417Barcode.PDF417Modes.Binary
objPDF417.DataToEncode = Code
objPDF417.XDimensionMILS = 24
objPDF417.ApplyTilde = true
objPDF417.PDFRows = 0
objPDF417.PDFColumns = 0
objPDF417.PDFErrorCorrectionLevel = 0
objPDF417.BMPPicture.Save(stream, System.Drawing.Imaging.ImageFormat.jpeg)
bitmapBytes = stream.ToArray
stream.Close()
Return bitmapBytes
End Function

QRCode:

Public Function GetQRCode(Code As String) as Byte()
dim stream as System.IO.MemoryStream = New System.IO.MemoryStream
dim bitmapBytes as Byte()
objQRCode.Resolution = 2
objQRCode.ResolutionCustomDPI = 96
objQRCode.ErrorCorrection = IDAutomation.Windows.Forms.QRCodeBarcode.ErrorCorrectionLevels.M
objQRCode.Version = IDAutomation.Windows.Forms.QRCodeBarcode.Versions.AUTO
objQRCode.EncodingMode = IDAutomation.Windows.Forms.QRCodeBarcode.EncodingModes.Byte
objQRCode.DataToEncode = Code
objQRCode.ApplyTilde = true
objQRCode.BMPPicture.Save(stream, System.Drawing.Imaging.ImageFormat.jpeg)
bitmapBytes = stream.ToArray
stream.Close()
Return bitmapBytes
End Function

SSRS Barcode Technical Support

Known SSRS Barcode Generation Issues

Additional Support Methods