|
Home > Products > .NET Barcode
> .NET, C#, VB.NET Barcode Componenet for Crystal Reports for ASP.NET
How to generate barcode images using Crystal Reports in ASP.NET website
Quick Navigate
- BarcodeLib.Barcode.dll
- Microsoft .NET Framework 2.0 (or later)
- Crystal Report for Visual Studio .NET 2005
-
Quick Introduction
- Create a new ASP.NET website
- Add httpHandlers for CrystalImageHandler.aspx on Web.Config
- Create a new Crystal Report Item, and set its data source
- In asp.net page, add Crystal Report Viewer, and set created report item to it.
- Call barcode library to get byte[] of barcode images.
- Done.
-
Create a new ASP.NET website.
- Create a new "website" project, named "BarcodeforCrystalReportsWebSite"
-
add the following code to Web.Config
<system.web>
<httpHandlers>
<add path="CrystalImageHandler.aspx" verb="GET" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
</httpHandlers>
...
</system.web>
-
Add a Crystal Report item to the project.
- Add a new item (Crystal Report), named "CustomerReport.rpt" to the project.
view image
- Select Expert as "Mail Label". Click "OK" button.
view image
- In "Mailing Labels Report Creation Wizard", click "Create New Connection", and expand "ADO.NET".
view image
- In "ADO.NET" form, select "CustomerDataSet.xsd" file in your downloaded package, and click "Finish" button.
view image
- In "Mailing Labels Report Creation Wizard", add table "Customer" under "ADO.NET", to selected table. And click "Next" button.
view image
- Add all three fields "ID", "CustomerId", "CustomerName", to "Fields to Display:". Click "Next".
view image
- Select default label "Return Address (Avery 5267)", and click "Finish" button.
- Now you can view the report template, and find that field "Barcode" not in the report template.
view image
- Re-arrange the report template.
view image
- Drag field "Barcode" to the report template. And Right click "Barcode" object, select "Format Object" menu.
view image
- In "Format Editor" form, check "Can Grow" property.
view image
-
Add Crystal Report Viewer to the Default.aspx.
- Add a reference to BarcodeLib.Barcode.dll
- Add the following code to the Default.aspx.cs file.
using System.Data.OleDb;
using System.Drawing.Imaging;
using BarcodeLib.Barcode.Linear;
- Add the following code to the Default.aspx.cs file.
protected void Page_Load(object sender, EventArgs e)
{
//create the database connection. Please change to correct data file (BarcodeDemoData.mdb) path.
OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/net_barcode_trial/Reporting/BarcodeDemoData.mdb");
aConnection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("SELECT * FROM Customer", aConnection);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
//Add the Barcode column to the DataSet
ds.Tables[0].Columns.Add(new DataColumn("Barcode", typeof(byte[])));
//Create an instance of Linear Barcode
//Use BarcodeLib.Barcode.DataMatrix.DataMatrix for Data Matrix
//Use BarcodeLib.Barcode.PDF417.PDF417 for PDF417
//Use BarcodeLib.Barcode.PDF417.MicroPDF417 for MicroPDF417
BarcodeLib.Barcode.Linear.Linear barcode = new BarcodeLib.Barcode.Linear.Linear();
//Barcode settings
barcode.Type = BarcodeLib.Barcode.Linear.BarcodeType.CODE128;
barcode.BarHeight = 50; //50 pixel
barcode.Format = System.Drawing.Imaging.ImageFormat.Png;
foreach (DataRow dr in ds.Tables[0].Rows)
{
barcode.Data = (int)dr["CustomerId"] + "";
byte[] imageData = barcode.drawBarcodeAsBytes();
dr["Barcode"] = imageData;
}
ReportDocument rdoc = new ReportDocument();
rdoc.Load(Server.MapPath("CustomerReport.rpt"));
rdoc.SetDataSource(ds);
this.CrystalReportViewer1.ReportSource = rdoc;
//close the connection Its important.
aConnection.Close();
}
-
Run the project.
|