RDLC Report Export On Server

We will write an RDLC report on the server silently without visibility to the user.

  1. Create a new asp.net website project,

    project

    Give name as “RdlcWriteOnServer”.

  2. Right click on project,

    add

    Add --> Add New Item --> Web Form.

  3. Add Web Form,

    web form

    Give file name as DEFAULT.ASPX.

  4. Now, Insert a new Report File (RDLC).

    rdlc

    Report file named as FRIENDREPORT.RDLC.

  5. Open your WEB.CONFIG file and set connection strings.
    1. <connectionStrings>  
    2.     <add name="RDLCConnectionString" connectionString="Data Source=SAIBABA-PC\SAIBABA;Initial Catalog=MemberCDAC;Integrated Security=True" providerName="System.Data.SqlClient" />  
    3. </connectionStrings>  
    Table Structure:

    1. USE[MBKTest]  
    2. GO  
    3. /****** Object: Table [dbo].[tblFriends] Script Date: 02/17/2016 11:05:19 ******/  
    4. SET ANSI_NULLS ON  
    5. GO  
    6. SET QUOTED_IDENTIFIER ON  
    7. GO  
    8. SET ANSI_PADDING ON  
    9. GO  
    10. CREATE TABLE[dbo].[tblFriends]  
    11. (  
    12.     [FriendID][int] IDENTITY(1, 1) NOT NULL, [Name][varchar](50) NULL, [Place][varchar](25) NULL, [Mobile][varchar](15) NULL, [EmailAddress][varchar](150) NULL  
    13. ON[PRIMARY]  
    14. GO  
    15. SET ANSI_PADDING OFF  
    16. GO  
    table

  6. Right click on proejct add a DATASET,

    dataset

    Give the name of file “FriendDataSet.xsd”

    As you add dataset file, system prompt you this message,

    message

    Select Yes,

    This is will generate a single DLL file of your DATASET and OTHER FILES.

  7. Now, double click on FREINDDATASET.XSD file.

    Right click on canvas of dataset file.

    dataset

    Select Add-->DataTable (To create a hardcoded datatable inside dataset).

  8. As you click on DataTable screen will look like this:

    datatable

    Single click on DataTable1 and rename to FriendDataTable.
    Right click on FriendDataTable and add new column or press CTRL+L .

    Add the following columns:

    • FriendID
    • Name
    • Place
    • Mobile
    • EmailAddress


  9. After adding the above said field FriendDataSet.xsd's FriendDataTable looks like this.

    friend data table

  10. Double click on RDLC file. (FriendReport.rdlc)

    rdlc

    On your left side you can see ToolBox containing three tabs.

    a. ReportData
    b. Server Explorer
    c. ToolBox

    If you can see ReportData toolbox Press CTRL + ALT + D to activate ReportData toolbox option. Click on Report Data Toolbox and Select DataSets,

    datasets

    properties

    In DataSource dropdownlist select your strong typed DATASET called FriendDataSet.

    In Available DataSets dropdownlist select your strongly typed DataTable called FriendDataTable.

    dataset1

  11. After selection of DataTable press ok. You can see Dataset1 populated with Fields.

  12. Now, click on ToolBox and Select Table tool Option. Drag and drop Table on the report canvas. Three columns are created by default. You can add more columns by simply right clicking.

    right click

  13. When you click o the table header and press right click you can addan extra column, it will display like this.

    column

  14. Report Header and Footer,

    Click on REPORT menu from menu bar.

    report

  15. You can see there is option to activate Add Page Header, Add Page Footer.

  16. Select ToolBox and Select TextBox tool and write, CSharpCorner Friend List as report title.

  17. Your report should look like this:

    report

    NOTE:

    While you export RDLC report as PDF format it expands the column size visually, so make it as reduced as you can and check export copy as per your report UI.

    IMPORTANT NOTE:

    In webform you can give DLL reference manually and write web.config code for report viewer.

    1. Drag N Drop Report viewer on DEFAULT.ASPX, it will auto write and upate your WEB.CONFIG file and reference in the code behind file.

    OR

    2. Writ the following code in WEB.CONFIG file and rebuild the solution.
    Under system.web section.  
    1. <compilation debug="true" targetFramework="4.5">  
    2.     <assemblies>  
    3.         <add assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" />  
    4.         <add assembly="Microsoft.ReportViewer.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91" />  
    5.         <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />  
    6.     </assemblies>  
    7. </compilation>  
  18. DEFAULT.ASPX code
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
    2.   
    3.     <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>  
    4.   
    5.         <!DOCTYPE html>  
    6.   
    7.         <html xmlns="http://www.w3.org/1999/xhtml">  
    8.   
    9.         <head runat="server">  
    10.             <title></title>  
    11.         </head>  
    12.   
    13.         <body>  
    14.             <form id="form1" runat="server">  
    15.                 <div>  
    16.   
    17.                 </div>  
    18.             </form>  
    19.         </body>  
    20.   
    21.         </html>  
  19. DEFAULT.ASPX.CS code behind,
    1. using Microsoft.VisualBasic;  
    2. using System;  
    3. using System.Collections;  
    4. using System.Collections.Generic;  
    5. using System.Data;  
    6. using System.Diagnostics;  
    7. using System.Configuration;  
    8. using System.Data.SqlClient;  
    9. using Microsoft.Reporting.WebForms;  
    10. using System.IO;  
    11. using Microsoft.ReportingServices;  
    12. partial class _Default: System.Web.UI.Page  
    13. {  
    14.   
    15.     protected void Page_Load(object sender, EventArgs e)  
    16.     {  
    17.   
    18.         CreatePDF();  
    19.   
    20.   
    21.     }  
    22.   
    23.   
    24.   
    25.     private string CreatePDF()  
    26.     {  
    27.         //DataSet  
    28.         FriendDataSet frndDataSet = new FriendDataSet();  
    29.   
    30.   
    31.         string constr = ConfigurationManager.ConnectionStrings["RDLCConnectionString"].ConnectionString;  
    32.   
    33.         SqlConnection con = new SqlConnection(constr);  
    34.         SqlDataAdapter da = new SqlDataAdapter("Select * From tblFriends", con);  
    35.         da.Fill(frndDataSet, "FriendDataTable");  
    36.   
    37.         // Create Report DataSource  
    38.         ReportDataSource datasource = new ReportDataSource("DataSet1", frndDataSet.Tables["FriendDataTable"].DefaultView);  
    39.   
    40.         // Variables  
    41.         Warning[] warnings = null;  
    42.         string[] streamids = null;  
    43.         string mimeType = null;  
    44.         string encoding = null;  
    45.         string extension = null;  
    46.   
    47.         // Setup the report viewer object and get the array of bytes  
    48.         ReportViewer viewer = new ReportViewer();  
    49.         viewer.ProcessingMode = ProcessingMode.Local;  
    50.         viewer.LocalReport.ReportPath = "FriendReport.rdlc";  
    51.         viewer.LocalReport.DataSources.Clear();  
    52.         viewer.LocalReport.DataSources.Add(datasource);  
    53.         viewer.LocalReport.EnableHyperlinks = true;  
    54.         viewer.ShowParameterPrompts = true;  
    55.   
    56.         viewer.LocalReport.Refresh();  
    57.   
    58.   
    59.         string path = @ "G:\Article\RdlcWriteOnServer\PDF\";  
    60.   
    61.         Guid id = Guid.NewGuid();  
    62.   
    63.         dynamic savePath2 = (path + "\\FriendList" + id.ToString() + ".pdf");  
    64.   
    65.         byte[] bytes = viewer.LocalReport.Render("PDF"nullout mimeType, out encoding, out extension, out streamids, out warnings);  
    66.   
    67.         FileStream file =  
    68.             default (FileStream);  
    69.   
    70.         file = new FileStream(savePath2, FileMode.Create);  
    71.         file.Write(bytes, 0, bytes.Length);  
    72.   
    73.         file.Close();  
    74.         file.Dispose();  
    75.   
    76.         return savePath2;  
    77.     }  
    78. }
  20. Run Application and check in folder G:\Article\RdlcWriteOnServer\PDF.

    folder

    folder


    Inthe next article I will explain How to EXPORT RDLC report at CLIENT / USER SIDE.