Beruflich Dokumente
Kultur Dokumente
In the following Step-By-Step Guide we're going to create a local report (RDLC file) which features barcoding capabilities by using
Barcode Professional for ASP.NET.
Follow these steps:
Open Visual Studio and create a new ASP.NET Website naming it LocalReportWithBarcode.
Report's Data Source settings
Data can be supplied to Local Reports either as custom Objects or as ADO.NET DataTables. In this guide we'll be
exploring each data source scenario in details. So please choose the data source scenario you want to use first and then
follow the steps for each of them:
o Using ADO.NET DataTables as data source of my local report
o Using Custom Objects as data source of my local report
Click Add button. You will be asked if you want to place the AdventureWorks.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 AdventureWorks 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, Name, ProductModel FROM Production.vProductAndDescription WHERE (CultureID = N'en')
After that, add a new custom Column to the DataTable just created and name it Barcode as is shown in the following
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.
The data source for the report should look like the following figure. NOTE: You can display Data Sources Window by
selecting Data menu and then Show Data Sources (Shift+Alt+D)
1º Column: It holds an Image item. Drag & drop an Image item into it and set its properties as is shown in the
following figure. Notice that Value property of the Image item is bound to the Barcode column.
The Image item's properties. The Value property is bound to Barcode Column.
Me.ReportViewer1.LocalReport.DataSources.Add(rptDataSource)
Me.ReportViewer1.LocalReport.ReportPath =
Server.MapPath("BarcodeReport.rdlc")
Me.ReportViewer1.LocalReport.Refresh()
End Sub
Visual C# .NET
this.ReportViewer1.LocalReport.DataSources.Add(rptDataSource);
this.ReportViewer1.LocalReport.ReportPath =
Server.MapPath("BarcodeReport.rdlc");
this.ReportViewer1.LocalReport.Refresh();
}
That's it. Run your application. You should get the barcode images displayed on the report.
ReportViewer control lets you to export the displayed report to Acrobat PDF as well as Microsoft Excel XLS. In both cases
the barcode images are maintained.
The local report featuring barcodes generated by Barcode Professional
TIP: How to export to Acrobat PDF the Local Report with Barcode images without Preview
The following walkthrough shows how to programmatically export a local report to PDF without viewing it.
o Add a new ASP.NET WebForm to the project and drag & drop a Button control onto it.
o Be sure your project is referencing the following assemblies:
Microsoft.ReportViewer.Common.dll
Microsoft.ReportViewer.WebForms.dll
Neodynamic.WebControls.BarcodeProfessional.dll
o Double click on the Button control and write the following code in the Button1_Click event procedure.
'Create ReportViewer
Dim viewer As New
Microsoft.Reporting.WebForms.ReportViewer()
viewer.LocalReport.DataSources.Add(rptDataSource)
viewer.LocalReport.ReportPath =
Server.MapPath("BarcodeReport.rdlc")
'Export to PDF
Dim pdfContent As Byte() =
viewer.LocalReport.Render("PDF", Nothing, Nothing, Nothing,
Nothing, Nothing, Nothing)
'Return PDF
Me.Response.Clear()
Me.Response.ContentType = "application/pdf"
Me.Response.AddHeader("Content-disposition",
"attachment; filename=BarcodeReport.pdf")
Me.Response.BinaryWrite(pdfContent)
Me.Response.End()
End Sub
Visual C# .NET
AdventureWorksTableAdapters.vProductAndDescriptionTableAdapt
er ta = new
AdventureWorksTableAdapters.vProductAndDescriptionTableAdapt
er();
AdventureWorks.vProductAndDescriptionDataTable dt = new
AdventureWorks.vProductAndDescriptionDataTable();
ta.Fill(dt);
Neodynamic.WebControls.BarcodeProfessional.BarcodeProfession
al bcp = new
Neodynamic.WebControls.BarcodeProfessional.BarcodeProfession
al();
//Barcode settings
bcp.Symbology =
Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
;
bcp.BarHeight = 0.25f;
//Create ReportViewer
Microsoft.Reporting.WebForms.ReportViewer viewer = new
Microsoft.Reporting.WebForms.ReportViewer();
viewer.LocalReport.DataSources.Add(rptDataSource);
viewer.LocalReport.ReportPath =
Server.MapPath("BarcodeReport.rdlc");
//Export to PDF
string mimeType;
string encoding;
string fileNameExtension;
string[] streams;
Microsoft.Reporting.WebForms.Warning[] warnings;
o That's it. Run your project opening the last WebForm and click on the button to generate the PDF version for the
barcode report.
The local report featuring barcodes exported to PDF format without previewing it onto the ASP.NET WebForm
You will be asked if you want to place the class file in the App_Code directory. Answer yes.
The Product class will feature basic info about products such is ID and Name as well as a special property called
Barcode which will hold the barcode image for each product object.
End Class
Visual C# .NET
public string Id
{
get
{
return m_id;
}
set
{
m_id = value;
}
}
Add another new class item to the project and name it Catalog. This new class Catalog will be used as the object data
source for our report. NOTE: In order for a class to qualify as an object data source, there should be a "Select method"
that returns a collection of objects of the involved class and thus why we'll create this Catalog class.
You will be asked if you want to place the class file in the App_Code directory. Answer yes.
The Catalog class will allow us to add Product objects and features a "Select method" called GetProducts which
returns a strongly typed collection of Product objects.
End Class
Visual C# .NET
public Catalog()
{
m_products = new
System.Collections.Generic.List<Product>();
}
Save the class file and go to Build menu and click on Build Solution
Now add a new Report item to the project and name it BarcodeReport.rdlc
Adding a local report (RDLC) to the project
The data source for the report should look like the following figure. NOTE: You can display Data Sources Window by
selecting Data menu and then Show Data Sources (Shift+Alt+D)
1º Column: It holds an Image item. Drag & drop an Image item into it and set its properties as is shown in the
following figure. Notice that Value property of the Image item is bound to the Barcode column.
The Image item's properties. The Value property is bound to Barcode Column.
For i = 1 To 10
'Random product info
prodId = Guid.NewGuid().ToString().Substring(0, 10)
prodName = "Product " + prodId
'Set the value to encode
bcp.Code = prodId
'Generate the barcode image and store it into the Barcode Column
prodBarcode =
bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)
Visual C# .NET
this.ReportViewer1.LocalReport.DataSources.Add(rptDataSource);
this.ReportViewer1.LocalReport.ReportPath =
Server.MapPath("BarcodeReport.rdlc");
this.ReportViewer1.LocalReport.Refresh();
}
That's it. Run your application. You should get the barcode images displayed on the report.
The local report in Acrobat PDF format featuring barcodes generated by Barcode Professional
TIP: How to export to Acrobat PDF the Local Report with Barcode images without Preview
The following walkthrough shows how to programmatically export a local report to PDF without viewing it.
o Add a new ASP.NET WebForm to the project and drag & drop a Button control onto it.
o Be sure your project is referencing the following assemblies:
Microsoft.ReportViewer.Common.dll
Microsoft.ReportViewer.WebForms.dll
Neodynamic.WebControls.BarcodeProfessional.dll
o Double click on the Button control and write the following code in the Button1_Click event procedure.
For i = 1 To 10
'Random product info
prodId = Guid.NewGuid().ToString().Substring(0, 10)
prodName = "Product " + prodId
'Set the value to encode
bcp.Code = prodId
'Generate the barcode image and store it into the Barcode Column
prodBarcode =
bcp.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png)
'Create ReportViewer
Dim viewer As New
Microsoft.Reporting.WebForms.ReportViewer()
viewer.LocalReport.DataSources.Add(rptDataSource)
viewer.LocalReport.ReportPath =
Server.MapPath("BarcodeReport.rdlc")
'Export to PDF
Dim pdfContent As Byte() =
viewer.LocalReport.Render("PDF", Nothing, Nothing, Nothing,
Nothing, Nothing, Nothing)
'Return PDF
Me.Response.Clear()
Me.Response.ContentType = "application/pdf"
Me.Response.AddHeader("Content-disposition",
"attachment; filename=BarcodeReport.pdf")
Me.Response.BinaryWrite(pdfContent)
Me.Response.End()
End Sub
Visual C# .NET
Neodynamic.WebControls.BarcodeProfessional.BarcodeProfession
al bcp = new
Neodynamic.WebControls.BarcodeProfessional.BarcodeProfession
al();
//Barcode settings
bcp.Symbology =
Neodynamic.WebControls.BarcodeProfessional.Symbology.Code128
;
bcp.BarHeight = 0.25f;
//Fill the object datasource.
//NOTE: You may connect to a SQL Server Database and
create Product objects based on data returned from such
source.
Catalog data = new Catalog();
//Create ReportViewer
Microsoft.Reporting.WebForms.ReportViewer viewer = new
Microsoft.Reporting.WebForms.ReportViewer();
//Export to PDF
string mimeType;
string encoding;
string fileNameExtension;
string[] streams;
Microsoft.Reporting.WebForms.Warning[] warnings;
//Return PDF
this.Response.Clear();
this.Response.ContentType = "application/pdf";
this.Response.AddHeader("Content-disposition",
"attachment; filename=BarcodeReport.pdf");
this.Response.BinaryWrite(pdfContent);
this.Response.End();
}
o That's it. Run your project opening the last WebForm and click on the button to generate the PDF version for the
barcode report.
The local report featuring barcodes exported to PDF format without previewing it onto the ASP.NET WebForm