How to integrate RDLC report into ASP.NET and generate PDF file

Programming, error messages and sample code > sample code
1. create an RDLC report in your project through Visual Studio > Project > right click on a folder > Add New Item > search "report", if you do not see the "Report" and "Report Wizard" options,
please make sure you installed the "Microsoft RDLC Report Designer" extension for your Visual Studio.
  • Report Wizard allows you to create data source and data set while creating the report file
  • Report allows you to create a report file and then set the data source and data set manually or programmatically
 
2. in this sample, I create a report file named "Report1.rdlc" and a WebForm page named "Default", it can also be used in an MVC application.
 
install Microsoft.ReportingServices.ReportViewerControl.WebForms using NuGet package manager or PM console
 
Install-Package Microsoft.ReportingServices.ReportViewerControl.WebForms
 
Default.aspx
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<%-- must add this assembly reference below --%>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<%-- must add this assembly reference above --%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Sample RDLC report</title>
</head>
<body>
    <form id="form1" runat="server">
        <%-- the ScriptManager belongs to AJAX extensions and ReportViewer belongs to Microsoft.ReportingServices.ReportViewerControl.WebForms NuGet package --%>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server"></rsweb:ReportViewer>
    </form>
</body>
</html>
 
Default.aspx.cs
 
using Microsoft.Reporting.WebForms;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = new DataSet();

                // replace the connection string with your true database information
                using (SqlConnection sqlconn = new SqlConnection("Data Source=XXX.site4now.net;Initial Catalog=XXX;User Id=XXX;Password=XXX"))
                {
                    // query to fetch data from database, repleace the query with your true SQL statements
                    SqlDataAdapter adap = new SqlDataAdapter("SELECT [column1], [column2] FROM [DB_name].[dbo].[table_name]", sqlconn);
                    adap.Fill(ds);
                }

                // set to local report mode
                ReportViewer1.ProcessingMode = ProcessingMode.Local;

                // load local report path, throw an error when report path is invalid
                ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report1.rdlc");

                // clear any default data source
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportDataSource dataSource = new ReportDataSource("DataSet1", ds.Tables[0]);
                ReportViewer1.LocalReport.DataSources.Add(dataSource);

                // generate PDF format file and store data stream in buffer
                byte[] buffer = ReportViewer1.LocalReport.Render(format: "PDF", deviceInfo: "");

                // save PDF file to physical path, replace the path with your true file path
                using (FileStream stream = new FileStream(Server.MapPath("~/output.pdf"), FileMode.Create))
                {
                    stream.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }
}