How to create, print barcodes in RDLC reports in C# ASP.NET web application?
Create new ASP.NET project
Start Visual Studio 2022 and select "Create a new project".
Select "ASP.NET Web Application (.NET Framework)" in the dialog and then press "Next" button.
Create a new project with name "OnBarcodeRDLCASPNETDemo".
And choose "Framework" as
Create an empty ASP.NET web application with default settings.
Now, all auto-generated files could be found in the solution explorer.
Select "ASP.NET Web Application (.NET Framework)" in the dialog and then press "Next" button.
Create a new project with name "OnBarcodeRDLCASPNETDemo".
And choose "Framework" as
.NET Framework 4.7.2.
Create an empty ASP.NET web application with default settings.
Now, all auto-generated files could be found in the solution explorer.
Add project NuGet packages and libraries
Right-click "References" in the Solution Explorer, and select "Manage NuGet Packages".
Select "Browse" and use the search control to find "Microsoft.ReportingServices.ReportViewerControl.WebForms" from the package source "nuget.org".
Choose the latest stable version to install the package.
Install "OnBarcode.RDLC" nuget package with version 10.0.1 or later.
Check "References" in the Solution Explorer to ensure the installation is success.
Select "Browse" and use the search control to find "Microsoft.ReportingServices.ReportViewerControl.WebForms" from the package source "nuget.org".
Choose the latest stable version to install the package.
Install "OnBarcode.RDLC" nuget package with version 10.0.1 or later.
Check "References" in the Solution Explorer to ensure the installation is success.
Tutorial 2: Create barcodes from database in RDLC report in C# ASP.NET project?
Add data source to project
Add a new item "DataSet1.xsd" to the project.
Insert a TableAdapter from Toolbox to "DataSet1.xsd" and configure it with TableAdapter Configuration Wizard.
Press "New Connection" to add a connection.
Set Server name to (LocalDB)\MSSQLLocalDB and select the restored database AdventureWorksLT2019. (Try "Test Connection" to ensure the connection is successful.) Then, press
Note:
Backup file of the Microsoft sample database AdventureWorks Lightweight (2019) can be downloaded from below website. https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver17&tabs=ssms
Check the checkbox to verify the connection string and press
Save the connection as AdventureWorksLT2019ConnectionString.
Use "Query Builder" to specify a SQL statement to access the database.
Add table "Product (SalesLT)" in the database and select two fields "ProductID" and "Name"; and then, press
Verify the SQL statement and Finish the wizard.
Add a new column to the data table "Product" with column name "Barcode" and set its property Data Type to
Finally, the data table "Product" contains three columns as below.
Insert a TableAdapter from Toolbox to "DataSet1.xsd" and configure it with TableAdapter Configuration Wizard.
Press "New Connection" to add a connection.
Set Server name to (LocalDB)\MSSQLLocalDB and select the restored database AdventureWorksLT2019. (Try "Test Connection" to ensure the connection is successful.) Then, press
OK to create the connection string.
Note:
Backup file of the Microsoft sample database AdventureWorks Lightweight (2019) can be downloaded from below website. https://learn.microsoft.com/en-us/sql/samples/adventureworks-install-configure?view=sql-server-ver17&tabs=ssms
Check the checkbox to verify the connection string and press
Next button to continue.
Save the connection as AdventureWorksLT2019ConnectionString.
Use "Query Builder" to specify a SQL statement to access the database.
Add table "Product (SalesLT)" in the database and select two fields "ProductID" and "Name"; and then, press
OK to finish.
Verify the SQL statement and Finish the wizard.
Add a new column to the data table "Product" with column name "Barcode" and set its property Data Type to
System.Byte[].
Finally, the data table "Product" contains three columns as below.
Design RDLC report (.rdlc) template
Add a new Report item "Report1.rdlc" to the project.
And change property "Copy to Output Directory" to
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "Product" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ProductID", "Name" and "Barcode" to this Table in the report.
Insert an "Image" to Barcode field, and set "Image Properties" as below.
Select the image source to
Finally, adjust table row height and column width to the appropriate size.
And change property "Copy to Output Directory" to
Copy if newer.
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "Product" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ProductID", "Name" and "Barcode" to this Table in the report.
Insert an "Image" to Barcode field, and set "Image Properties" as below.
Select the image source to
Database and use MIME type image/png for field Barcode.
Finally, adjust table row height and column width to the appropriate size.
Add control and behind C# code to the Web Form
Add a new Web Form item "Default.aspx" to the project
Add a ScriptManager and a ReportViewer control (with ID
Replace behind code class Default.aspx.cs by following codes.
Add a ScriptManager and a ReportViewer control (with ID
ReportViewer1) to the Web Form (id=form1) in the ASPX file.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="OnBarcodeRDLCASPNETDemo.Default" %> <%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>OnBarcode RDLC ASP.NET Demo (AdventureWorksLT2019)</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <rsweb:ReportViewer ID="ReportViewer1" Width="1000px" runat="server" /> </div> </form> </body> </html>
Replace behind code class Default.aspx.cs by following codes.
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.Reporting.WebForms; using OnBarcode.Barcode.RDLC; namespace OnBarcodeRDLCASPNETDemo { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Configure Report ReportDataSource rds = new ReportDataSource("DataSet1", GetData()); this.ReportViewer1.LocalReport.DataSources.Clear(); this.ReportViewer1.LocalReport.DataSources.Add(rds); this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report1.rdlc"); this.ReportViewer1.LocalReport.Refresh(); } } private DataTable GetData() { // Get data by using TableAdapter DataSet1TableAdapters.ProductTableAdapter adapter = new DataSet1TableAdapters.ProductTableAdapter(); DataTable dt = adapter.GetData(); Linear linear = new Linear(); linear.Type = BarcodeType.CODE128; linear.BarcodeWidth = 500; linear.BarcodeHeight = 100; foreach (DataSet1.ProductRow row in dt.Rows) { linear.Data = row.ProductID.ToString(); linear.OutputFileFormat = FileFormat.PNG; row.Barcode = linear.drawBarcodeAsBytes(); } return dt; } } }
Web.config configuration file
Ensure all settings in the Web.config file are correct.
<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="AdventureWorksLT2019ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=AdventureWorksLT2019;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.7.2"> <buildProviders> <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> </buildProviders> <assemblies> <add assembly="Microsoft.ReportViewer.Common, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> <add assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> </assemblies> </compilation> <httpRuntime targetFramework="4.7.2" /> <httpHandlers> <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" validate="false" /> </httpHandlers> </system.web> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true" /> <handlers> <add name="ReportViewerWebControlHandler" verb="*" path="Reserved.ReportViewerWebControl.axd" preCondition="integratedMode" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> </handlers> </system.webServer> </configuration>
Run the RDLC report in ASP.NET application
It is done. Now press Ctrl+F5 to run the project.
Screenshot for the RDLC report demo in ASP.NET application.
Screenshot for the RDLC report demo in ASP.NET application.
Tutorial 2: Print barcodes from DataTable in RDLC report in C# ASP.NET?
Add data source to project
Add a new item "DataSet1.xsd" to the project.
Add a new data table to "DataSet1.xsd". By default, the table name is "DataTable1".
Add a new column to the data table "DataTable1" with column name "ID" and set its property Data Type to "System.String".
Add another column to the table with name "Barcode" and set its property Data Type to "System.Byte[]"
Finally, the data table "DataTable1" contains two columns as below.
Add a new data table to "DataSet1.xsd". By default, the table name is "DataTable1".
Add a new column to the data table "DataTable1" with column name "ID" and set its property Data Type to "System.String".
Add another column to the table with name "Barcode" and set its property Data Type to "System.Byte[]"
Finally, the data table "DataTable1" contains two columns as below.
Design RDLC report
Add a new Report item "Report1.rdlc" to the project.
And change property "Copy to Output Directory" to "Copy if newer".
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "DataTable1" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ID" and "Barcode" to this Table in the report designer.
Insert an image to Barcode field, and set image properties as below.
In "Image Properties", select the image source to Database and use MIME type image/png for field Barcode.
Finally, adjust table row height and column width to the appropriate size.
And change property "Copy to Output Directory" to "Copy if newer".
Select "Add Dataset" to open the Dataset Properties dialog.
Choose "DataSet1" in the combo-box Data source and "DataTable1" in the combo-box Available datasets.
Insert a Table to the report designer.
Drag fields "ID" and "Barcode" to this Table in the report designer.
Insert an image to Barcode field, and set image properties as below.
In "Image Properties", select the image source to Database and use MIME type image/png for field Barcode.
Finally, adjust table row height and column width to the appropriate size.
Add Web Form to ASP.NET project
Add a new Web Form item "Default.aspx" to the project
Add a ScriptManager and a ReportViewer control (with ID "ReportViewer1") to the Web Form (id="form1") in the ASPX file.
Replace behind code class "Default.aspx.cs" by following codes.
Add a ScriptManager and a ReportViewer control (with ID "ReportViewer1") to the Web Form (id="form1") in the ASPX file.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="OnBarcodeRDLCASPNETDemo.Default" %> <%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>OnBarcode RDLC ASP.NET Demo</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <rsweb:ReportViewer ID="ReportViewer1" Width="1000px" runat="server" /> </div> </form> </body> </html>
Replace behind code class "Default.aspx.cs" by following codes.
Default.aspx.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.Reporting.WebForms; using OnBarcode.Barcode.RDLC; namespace OnBarcodeRDLCASPNETDemo { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Prepare report data. DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(String)); dt.Columns.Add("Barcode", typeof(byte[])); String id1 = "000001"; String id2 = "000002"; // Create data for the 1st row in the table. Linear linear = new Linear(); linear.Type = BarcodeType.CODE128; linear.BarcodeWidth = 600; linear.BarcodeHeight = 100; linear.OutputFileFormat = FileFormat.PNG; linear.Data = id1; byte[] imgData = linear.drawBarcodeAsBytes(); dt.Rows.Add(id1, imgData); // Create data for the 2nd row in the table. linear.Data = id2; imgData = linear.drawBarcodeAsBytes(); dt.Rows.Add(id2, imgData); // Configure Report ReportDataSource rds = new ReportDataSource("DataSet1", dt); this.ReportViewer1.LocalReport.DataSources.Clear(); this.ReportViewer1.LocalReport.DataSources.Add(rds); this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report1.rdlc"); this.ReportViewer1.LocalReport.Refresh(); } } } }
Web.config configuration file
Copy ALL contents in configuration tag
system.web and system.webServer (in red) to the Web.config file.
<?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.7.2"> <assemblies> <!-- All assemblies updated to version 15.0.0.0. --> <add assembly="Microsoft.ReportViewer.Common, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> <add assembly="Microsoft.ReportViewer.DataVisualization, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> <add assembly="Microsoft.ReportViewer.Design, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> <add assembly="Microsoft.ReportViewer.ProcessingObjectModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> <add assembly="Microsoft.ReportViewer.WebDesign, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> <add assembly="Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> <add assembly="Microsoft.ReportViewer.WinForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> </assemblies> <buildProviders> <!-- Version updated to 15.0.0.0. --> <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" /> </buildProviders> </compilation> <httpRuntime targetFramework="4.7.2" /> <httpHandlers> <!-- Version updated to 15.0.0.0 --> <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" validate="false" /> </httpHandlers> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true" /> <handlers> <!-- Version updated to 15.0.0.0 --> <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> </handlers> </system.webServer> </configuration>
Run the RDLC report in ASP.NET application
It is done. Now press Ctrl+F5 to run the project.
Screenshot for the RDLC report demo in ASP.NET application.
Screenshot for the RDLC report demo in ASP.NET application.
