Encoding, Adjusting &Printing Barcodes for RDLC Reports in C#.NET IDEs
KA.Barcode for RDLC Reports Overview
KeepAutomation Barcode Generator for RDLC is a standard and powerful barcode component that lets you integrate barcode generation and printing features into RDLC Reports. The easy-to-use .NET SDK could be managed in .NET with Visual C#. Windows applications as well as ASP.NET Web Forms are supported. This page will show you how to generated barcodes in RDLC using C#.NET.
Compatibility & Requirements
- .NET Development Environments: Visual Studio 2005/2008 /2010 installed, .NET Version greater than 2.0, C#.NET & VB.NET
- Compatible Operating Systems: Windows 2000, XP, Vista, and Windows 7
- Minimum System Requirements: 500 MHZ processor, 128 MB RAM, 5 MB available hard drive space
- Microsoft Windows XP
- Microsoft Visual Studio 2005
- Visual C#.NET
How to Generate Barcodes in RDLC in WinForms Using C#
- Open your Visual Studio and create a new WinForms project, then create a "DataSet" with the name "AdventureWorks.xsd".
- Drag "TableAdapter" item under the "Toolbox" to the new "DataSet".
- Link the connection to SQL Server AdventureWorks Sample Database.
- Copy "SELECT ProductID, Name FROM Production.vProductAndDescription WHERE (CultureID = N'en') " as SQL Statement.
- Right-click "vProductAndDescription" on the dataset to create a column (named as "Barcode"), and change the data type to "System.Byte[]" in "Properties" window.
- Go to "Solution Explorer" and create a new "Report" item.
- Insert a table to your report and add three columns in the dataset to the report table details section.
- Drag and drop "Image" item to the last column and name it "Barcode".
- Change "Source", "MIMEType", "Value" to "Database", "image/jpeg", "=Fields!Barcode.Value" respectively through "Properties" window.
- Drag "ReportViewer" under "Toolbox" to Form1.
- Click "Report1.rdlc[Design]", then click "Report" and choose "Data Sources....", click "Add to Report" & lastly click "OK".
- Add reference "KeepAutomation.Barcode.RDLC.dll" to your project.
- Right click "Form1.cs" and select "View Code", then compile the following sample code.
- Use "KeepAutomation.Barcode.RDLC" namespace and debug.
C# Sample code
private void Form1_Load(object sender, EventArgs e)
{
// load data to the data table this.vProductAndDescriptionTableAdapter.Fill
this.vProductAndDescriptionTableAdapter.Fill(this.AdventureWorks.vProductAndDescription);
// create a linear barcode object
BarCode barcode = new BarCode();
// set barcode type
barcode.Symbology = KeepAutomation.Barcode.Symbology.Code11;
// draw barcodes for each data row
foreach (AdventureWorks.vProductAndDescriptionRow
row in this.AdventureWorks.vProductAndDescription.Rows)
{// set barcode encoding data value
barcode.CodeToEncode = row.ProductID.ToString();
// set drawing barcode image format
barcode.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
row.Barcode = barcode.generateBarcodeToByteArray();
}
this.reportViewer1.RefreshReport();
}
How to Generate Barcodes in RDLC in Web Forms Using C#
- Open your Visual Studio and create a website project, and add "KeepAutomation.Barcode.RDLC.dll" to reference.
- Right click your project In "Solution Explorer" to add a new "Class" item.
- Copy the sample code into the "Class" item. Go to "Build" tab and choose "Build Web Site".
- Go to "Solution Explorer" and right click your web project to add a new "Report" item.
- Go to "Toolbox" and drag "Table" in "Report Items" to the report. Then, go to "Website Data Sources" tab and insert "BarcodeName" and "BarcodeId".
- Insert "Image" item to the last column and name it as "Barcode".
- Revise "Source" to "Database", "MIMEType" to "image/jpeg", "Value" to "=Fields!Byte.Value" respectively in "Properties" window.
- In "Solution Explorer", right click "Default.aspx" and select "View Designer" to add a "ReportViewer" to the project
- Choose your created report and run your ASP.NET web project.
C# Sample code
public class BarocdeDemo
{
private string m_barcodeName;
private int m_barcodeId;
private byte[] m_byte;
public BarocdeDemo(string barcodeName, int barcodeId, string data)
{
m_barcodeName = barcodeName;
m_barcodeId = barcodeId;
BarCode barcode = new BarCode();
barcode.Symbology = KeepAutomation.Barcode.Symbology.EAN13;
barcode.CodeToEncode = data;
m_byte = barcode.generateBarcodeToByteArray();
}
public byte[] Byte
{
get { return m_byte; }
}
public string BarcodeName
{
get { return m_barcodeName; }
}
public int BarcodeId
{
get { return m_barcodeId; }
}
}
public class Merchant
{
private List<BarocdeDemo> m_barcodes;
public Merchant()
{
m_barcodes = new List<BarocdeDemo>();
m_barcodes.Add(new BarocdeDemo("code11", 001, "code11"));
m_barcodes.Add(new BarocdeDemo("code25", 002, "code25"));
m_barcodes.Add(new BarocdeDemo("code39", 003, "code39"));
m_barcodes.Add(new BarocdeDemo("code93", 004, "code93"));
m_barcodes.Add(new BarocdeDemo("code128",005, "code128"));
m_barcodes.Add(new BarocdeDemo("upcean", 006, "upcean"));
}
public List<BarocdeDemo> GetProducts()
{
return m_barcodes;
}
}