Display (Render) RDLC Report in browser in ASP.Net MVC

Last Reply one year ago By dharmendr

Posted one year ago

Hello everyone,

I have a C# MVC application when I generate 1 report from a record it loads and works perfectly.

When I generate 2 or more reports that is supposed to open in the same PDF window they end up displaying on top of each other.

I'm not exactly sure why this is happening I have the page set 8.5 by 11 and correct side and ConsumeContainerWhitespace = true.

In the report my margin is ,,, so all 0.

Maybe one of you will see something that should be changed.

Here is my code.

case "Generate All":
    LocalReport localReport = new LocalReport()
    {
        ReportPath = Server.MapPath("~/ReportForms/VirtualService3.rdlc")
    };                                                            
    ReportDataSource rds = new ReportDataSource("Service_Fields", model.ListQuotes); 
    localReport.DataSources.Add(rds);

    string reportType = "PDF";
    string mimeType, encoding, fileNameExtension;

    string deviceInfo =
        "<DeviceInfo>" +
        "   <OutputFormat>"+reportType+"</OutputFormat>" +
        "   <PageWidth>8.5in</PageWidth>" +
        "   <PageHeight>11in</PageHeight>" +
        "   <MarginTop>0.5in</MarginTop>" +
        "   <MarginLeft>0in</MarginLeft>" +
        "   <MarginRight>0in</MarginRight>" +
        "   <MarginBottom>0in</MarginBottom>" +
        "</DeviceInfo>";

    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;
    //localReport.GetParameters();

    renderedBytes = localReport.Render(
        reportType,
        deviceInfo,
        out mimeType,
        out encoding,
        out fileNameExtension,
        out streams,
        out warnings);                   

    return File(renderedBytes, mimeType);
You are viewing reply posted by: dharmendr one year ago.
Posted one year ago

Hi bigbear,

I have checked your code. Its working.

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

Namespaces

using System.Linq;
using Microsoft.Reporting.WebForms;

Controller

public class HomeController : Controller
{
    // GET: /Home/Export
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Export()
    {
        NorthwindEntities entities = new NorthwindEntities();
        LocalReport localReport = new LocalReport()
        {
            ReportPath = Server.MapPath("~/CustomerReport.rdlc")
        };
        ReportDataSource rds = new ReportDataSource("Customers", entities.Customers.ToList());
        localReport.DataSources.Add(rds);

        string reportType = "PDF";
        string mimeType, encoding, fileNameExtension;

        string deviceInfo =
                        "<DeviceInfo>" +
                        "   <OutputFormat>" + reportType + "</OutputFormat>" +
                        "   <PageWidth>8.5in</PageWidth>" +
                        "   <PageHeight>11in</PageHeight>" +
                        "   <MarginTop>0.5in</MarginTop>" +
                        "   <MarginLeft>0in</MarginLeft>" +
                        "   <MarginRight>0in</MarginRight>" +
                        "   <MarginBottom>0in</MarginBottom>" +
                        "</DeviceInfo>";

        Warning[] warnings;
        string[] streams;
        byte[] renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding,
                                                    out fileNameExtension, out streams, out warnings);

        return File(renderedBytes, mimeType);
    }
}

View

<%using (Html.BeginForm("Export", "Home", FormMethod.Post)){%>
    <input type="submit" value="View Report" />
<%} %>

Screenshot