Using MS Report Viewer in ASP.NET MVC3

This article I  will introduce step by step how to use Microsoft Report Viewer in ASP.NET MVC3

1. Create a new project with View engine is ASPX and set named MvcApplications.MSReportViewer

image

2. Add references Microsoft.ReprotViewer.Common and Microsoft.ReportViewer.WebForms to project. You can find two .dll file in  “C:\Program Files (x86)\Microsoft Visual Studio 10.0\ReportViewer”.

3. Create two classes Customer.cs and CustomerModel under Models folder

public class Customer
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }

        public string Place { get; set; }
    }
public class CustomerModel
    {
        public virtual List GetAllCustomer()
        {
            List customerList = new List {
                new Customer { Id = 1, Name = "Patrick", Address = "Geuzenstraat 29", Place = "Amsterdam" },
                new Customer { Id = 2, Name = "Fred", Address = "Flink 9a", Place = "Rotterdam" },
                new Customer { Id = 3, Name = "Sjonnie", Address = "Paternatenplaats 44", Place = "Enkhuizen" },
                new Customer { Id = 4, Name = "Henk", Address = "Wakerdijk 74", Place = "Utrecht" },
                new Customer { Id = 5, Name = "Klaas", Address = "Paternatenplaats 44", Place = "Plaantan" },
                    new Customer { Id = 6, Name = "Andre", Address = "Wolbrantskerkweg 90B ", Place = "Los Angeles" },
                    new Customer { Id = 7, Name = "Pieter", Address = "Sam van Houtenstraat 191H", Place = "Emmen" },
                    new Customer { Id = 8, Name = "Sjohn", Address = "Polostraat, M. 103-II", Place = "Kantens" },
                    new Customer { Id = 9, Name = "John", Address = "Paternatenplaats 44", Place = "Leiden" },
                    new Customer { Id = 10, Name = "Bruin", Address = "Anthony Spatzierhof 9", Place = "Maasbracht" },
                    new Customer { Id = 11, Name = "Sjonnie", Address = "Van Heuven Goedhartlaan 201", Place = "Potters" },
                    new Customer { Id = 12, Name = "Lumunon", Address = "Paternatenplaats 44", Place = "Utrecht" },
                    new Customer { Id = 13, Name = "Friese", Address = "Burgemeester Roellstr 243-4", Place = "Rotterdam" },
                    new Customer { Id = 14, Name = "Hudephol", Address = "Bilhamerstraat 4", Place = "Vlaardingen" },
                    new Customer { Id = 15, Name = "Postema", Address = "Mastbos 77", Place = "Schiedam" },
                    new Customer { Id = 16, Name = "Scharn", Address = "Marius Bauerstraat 123", Place = "Dordrecht" },
                    new Customer { Id = 17, Name = "Wagenmakers", Address = "Pieter Postsingel 16", Place = "Ede" },
                    new Customer { Id = 18, Name = "Gedikli", Address = "Burgemeester Hogguerstr 283", Place = "Twello" },
                    new Customer { Id = 19, Name = "Zwollo", Address = "Meervalweg 140", Place = "Terschelling" },
                    new Customer { Id = 20, Name = "Sjonnie", Address = "Ruys de Beerenbrouckstr 79A", Place = "Ter Aar" },
                    new Customer { Id = 21, Name = "Schimmelmann", Address = "Ritzema Bosstraat 28-2", Place = "Vierenman" },
                    new Customer { Id = 22, Name = "Makhlouf", Address = "Ln vd Helende Meesters 12", Place = "Eindhoven" },
                    new Customer { Id = 23, Name = "Meyer", Address = "Burgemeester v Leeuwenln 79H", Place = "Breda" },
            };

            return customerList;
        }
    }

4. Create a folder and set named Reports.  After that create a new rptCustomer.rdlc file under this folder.

image

5. In Report Data window create new Dataset… and set property the same picture below

image

6. rptCustomer.rdlc file after designed:

image

7. Define action PrintReport in HomeController

public ActionResult PrintReport()
        {
            LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("~/Reports/rptCustomer.rdlc");
            ReportDataSource reportDataSource = new ReportDataSource("CustomerDataSet",
                new CustomerModel().GetAllCustomer());
            localReport.DataSources.Add(reportDataSource);

            string reportType = "pdf";
            string mimeType;
            string encoding;
            string fileNameExtension;
            //The DeviceInfo settings should be changed based on the reportType             
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx             
            string deviceInfo =
                "<DeviceInfo>" +
                "  <OutputFormat>PDF</OutputFormat>" +
                "  <PageWidth>8.5in</PageWidth>" +
                "  <PageHeight>11in</PageHeight>" +
                "  <MarginTop>0.5in</MarginTop>" +
                "  <MarginLeft>1in</MarginLeft>" +
                "  <MarginRight>1in</MarginRight>" +
                "  <MarginBottom>0.5in</MarginBottom>" +
                "</DeviceInfo>";
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
            //Render the report             
            renderedBytes = localReport.Render(
                reportType,
                deviceInfo,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);
            //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension);             
            return File(renderedBytes, mimeType);
        }

8. Add ActionLink in Index view

<p>
    <%: Html.ActionLink("PrintReport", "PrintReport", "Home")%>
</p>

9. Build and Run website.

image

10. Click PrinReport

image image

Advertisements
This entry was posted in ASP.NET MVC. Bookmark the permalink.

6 Responses to Using MS Report Viewer in ASP.NET MVC3

  1. Pingback: ASP.NET MVC 3: Save .pdf file to database and download pdf file from databse. | quysnhat's blog

  2. kedarpunde says:

    Line 43: byte[] renderedBytes;
    Line 44: //Render the report
    Line 45: renderedBytes = localReport.Render(
    Line 46: reportType,
    Line 47: deviceInfo,

    hello thanks for this article but i am getting error here.
    i implemented your code as it is.
    can you please help me out.

    • kedarpunde says:

      in your example what is
      ReportDataSource reportDataSource = new ReportDataSource(“CustomerDataSet”,
      new CustomerModel().GetAllCustomer());
      CustomerModel()
      GetAllCustomer()
      i am getting Error here
      An error occurred during local report processing.
      renderedBytes = localReport.Render(
      reportType,
      deviceInfo,
      out mimeType,
      out encoding,
      out fileNameExtension,
      out streams,
      out warnings
      );

      Why it is

  3. Yan says:

    In steps 5: In Report Data window create new Dataset… and set property the same picture below
    How to set the properties like that?

  4. Johnk689 says:

    You are a really persuasive writer. I can see this in your writeup. You’ve a way of writing compelling info that sparks significantly interest. edekdadkaeee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s