In this article I will explain with an example, how to export RDLC Report to PDF programmatically in ASP.Net with C# and VB.Net.
Exporting RDLC Report to PDF programmatically means, exporting from Code-Behind without using the RDLC ReportViewer control in ASP.Net.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
HTML Markup
The HTML Markup consists of a RDLC ReportViewer control, a RadioButtonList with the Export file formats and a Button to export RDLC Reports to Word, Excel, PDF and Image file formats.
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="500" SizeToReportContent = "true">
    </rsweb:ReportViewer>
    <br />
    Format:
    <asp:RadioButtonList ID="rbFormat" runat="server" RepeatDirection="Horizontal">
        <asp:ListItem Text="Word" Value="WORD" Selected="True" />
        <asp:ListItem Text="Excel" Value="EXCEL" />
        <asp:ListItem Text="PDF" Value="PDF" />
        <asp:ListItem Text="Image" Value="IMAGE" />
    </asp:RadioButtonList>
    <br />
    <asp:Button ID="btnExport" Text="Export" runat="server" OnClick="Export" />
    </form>
</body>
</html>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports Microsoft.Reporting.WebForms
 
 
Designing and populating the RDLC Report from Database
Inside the Page Load event, the RDLC Report is populated from database.
Note: For more details about designing and populating RDLC Report, please refer the following article.
         RDLC (Local SSRS) Report ASP.Net Example
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
        Customers dsCustomers = GetData("SELECT TOP 10 * FROM Customers");
        ReportDataSource datasource = new ReportDataSource("Customers", dsCustomers.Tables[0]);
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
    }
}
 
private Customers GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
 
            sda.SelectCommand = cmd;
            using (Customers dsCustomers = new Customers())
            {
                sda.Fill(dsCustomers, "DataTable1");
                return dsCustomers;
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        ReportViewer1.ProcessingMode = ProcessingMode.Local
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc")
        Dim dsCustomers As Customers = GetData("SELECT TOP 10 * FROM Customers")
        Dim datasource As New ReportDataSource("Customers", dsCustomers.Tables(0))
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(datasource)
    End If
End Sub
 
Private Function GetData(query As String) As Customers
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim cmd As New SqlCommand(query)
    Using con As New SqlConnection(conString)
        Using sda As New SqlDataAdapter()
            cmd.Connection = con
 
            sda.SelectCommand = cmd
            Using dsCustomers As New Customers()
                sda.Fill(dsCustomers, "DataTable1")
                Return dsCustomers
            End Using
        End Using
    End Using
End Function
 
 
Exporting RDLC Report to PDF programmatically in ASP.Net
When the Export Button is clicked, the selected value of the RadioButtonList is passed to the Render method of RDLC ReportViewer.
The Render method returns the exported document in Byte Array format.
Finally, the Byte Array is written to Response Stream and downloaded as Attachment in Word, Excel, PDF or Image files.
C#
protected void Export(object sender, EventArgs e)
{
    Warning[] warnings;
    string[] streamIds;
    string contentType;
    string encoding;
    string extension;
 
    //Export the RDLC Report to Byte Array.
    byte[] bytes = ReportViewer1.LocalReport.Render(rbFormat.SelectedItem.Value, null, out contentType, out encoding, out extension, out streamIds, out warnings);
 
    //Download the RDLC Report in Word, Excel, PDF and Image formats.
    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = contentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=RDLC." + extension);
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
    Dim warnings As Warning()
    Dim streamIds As String()
    Dim contentType As String
    Dim encoding As String
    Dim extension As String
 
    'Export the RDLC Report to Byte Array.
    Dim bytes As Byte() = ReportViewer1.LocalReport.Render(rbFormat.SelectedItem.Value, Nothing, contentType, encoding, extension, streamIds, warnings)
 
    'Download the RDLC Report in Word, Excel, PDF and Image formats.
    Response.Clear()
    Response.Buffer = True
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = contentType
    Response.AppendHeader("Content-Disposition", "attachment; filename=RDLC." & extension)
    Response.BinaryWrite(bytes)
    Response.Flush()
    Response.End()
End Sub
 
 
Screenshots
The RDLC Report
Export RDLC Report to PDF programmatically in ASP.Net
 
RDLC Report exported to PDF file
Export RDLC Report to PDF programmatically in ASP.Net
 
 
Demo
 
 
Downloads