Rendering RDLC to pdf output console application

11,838

Okay to do it programatically the simplest solution is:

Fill a DataTable with the Data for the Report and name the Datatable "Sales" (like the DataSource name in your report.

Please note this is pseudo code which will not work but should give you an idea.

var myDataSet = new DataSet(); 
var da = new SqlDataAdapter("Select * from Sales", "yourConnectionstring");

da.Fill(myDataSet);
DataTable table = myDataSet.Tables[0];
table.TableName = "Sales";

Add the DataTable as a Datasource to your report:

viewer.LocalReport.DataSources.Add(table);
Share:
11,838
Murali Uppangala
Author by

Murali Uppangala

Updated on June 04, 2022

Comments

  • Murali Uppangala
    Murali Uppangala almost 2 years

    I have referred some articles in this website for Rendering .rdlc to .pdf output using console application.Am new to C# .net built a application for the same as below gives an error saying :>Rdclrender.exe!Rdclrender.Program.Main(string[] args = {string[0]}) Line 28 My class is given below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Reporting.WinForms;
    
    
    namespace Rdclrender
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Variables
                Warning[] warnings;
                string[] streamIds;
                string mimeType = string.Empty;
                string encoding = string.Empty;
                string extension = string.Empty;
    
    
                // Setup the report viewer object and get the array of bytes
                ReportViewer viewer = new ReportViewer();
                viewer.ProcessingMode = ProcessingMode.Local;
                viewer.LocalReport.ReportPath = "Report.rdlc";
    
    
                byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
    
                using (System.IO.FileStream fs = new System.IO.FileStream("output.pdf", System.IO.FileMode.Create))
                {
                    fs.Write(bytes, 0, bytes.Length);
                }
                // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
                /*  Response.Buffer = true;
                  Response.Clear();
                  Response.ContentType = mimeType;
                  Response.AddHeader("content-disposition", "attachment; filename=" + fileName + "." + extension);
                  Response.BinaryWrite(bytes); // create the file
                  Response.Flush(); // send it to the client to download*/
            }
        }
    }
    

    Is this is the way to create pdf from .rdl? I had renamed my .rdl to .rdlc manually an added .rdlc item to project.