0

//My Controller.
public ActionResult ExpectedSales(string[] Quarter, int Year, string[] Divisions)
        {
            
                try
                {
                    int myQtr = GFIHelper.GetQuarterNumber(Quarter);
                    string ProductDivisions = Repository.getMyDivision(@User.Identity.Name, Divisions);
                    
                    //All ReportViewer parameters are returned with GFISSRSReportViewer.GetReportViewer();
                    //.....................................................................
                    ReportViewer MyReportViewer = GFISSRSReportViewer.GetReportViewer();
                    MyReportViewer.ServerReport.ReportPath = GFIReportsPath.GetSalesManagementReportURL("ExpectedSales");
                    //.....................................................................
                  
                    
                  
                    //GFISSRSReportViewer.GetCommonParaList includes only PrintedDate.
                    //.....................................................................
                    List<ReportParameter> paramList = GFISSRSReportViewer.GetCommonParaList(GFIHelper.TitleCase(User.Identity.Name));
                    //.....................................................................
                    string nextyear = "";
                    if (myQtr == 1)
                        nextyear = Convert.ToString(Year + 0);
                    else
                        nextyear = Convert.ToString(Year + 1);
                    //Printedby and report name must be assigned here
                    //.....................................................................
                    string rptname = "EXPECTED SALES" + "@" + string.Join("+", Divisions) + "@" + string.Join("+", Quarter) + " : " + Year + (Year == Convert.ToInt32(nextyear) ? "" : " - " + nextyear);
                    rptname = rptname.Replace("@", Environment.NewLine);
                    paramList.Add(new ReportParameter("ReportName", rptname, false));
                    //.....................................................................

                    paramList.Add(new ReportParameter("YearPara", Convert.ToString(Year), false));
                    paramList.Add(new ReportParameter("QuarterStringPara", Convert.ToString(myQtr), false));
                    paramList.Add(new ReportParameter("DivisionsPara", ProductDivisions, false));

                    MyReportViewer.ServerReport.SetParameters(paramList);

                    
                    ViewBag.ReportViewer = MyReportViewer;
                    return PartialView("_ReportLayout");
                }
                catch (Exception ex)
                {
                    return PartialView(ex.Message + ex.StackTrace);
                }
            
        }
        
//My Layout for Report

@using ReportViewerForMvc;
@using System.Web.UI.WebControls;

<br />
<br />
<div class="myReportViewer md-block" flex="100" flex-gt-sm>
    <div class="myReportViewerHead">
        <span>Report Viewer</span>
    </div>
    <md-divider style="background-color:orange;"></md-divider>
    @if (ViewBag.ReportViewer != null)
    {
        @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer, new
               {
                   frameBorder = "0",
                   Width = Unit.Percentage(100),
                   Height = 600,
                   ScrollBars="Yes",
                   SizeToReportContent=true
               })
    }
</div>

I have been working on a C# / ASP.NET MVC 5 / AngualrJs project with SQL Server 2008 R2 Reporting Server (.rdlc). I actually want to bind dataset from inside ASP.NET MVC to the ReportViewer object. But I don't know exactly how to accomplish this.

Can anyone help me with this?

Currently my reports are using datasets from inside the VS Report Server project and working fine, but I just need to pass the dataset from C# code.

Thanks.

Scavenger
  • 47
  • 2
  • 9

2 Answers2

0

You can use the Nuget package ReportViewerForMvc to include the ReportViewer in your Razor views.

After you install the package, you can create a view (e.g. Report1.cshtml) which could look something like this:

@using ReportViewerForMvc;
@if (ViewBag.ReportViewer != null)
{
    @Html.ReportViewer(ViewBag.ReportViewer as Microsoft.Reporting.WebForms.ReportViewer)
}

In your controller action result, you have to pass the ViewBag.ReportViewer object which you must instantiate and give the data source.

public ActionResult Report1()
{
    IEnumerable<Report1DataSet> entities = repo.GetReport1DataSet(); // I am fetching the entities from a repository
    ReportViewer rptViewer = new ReportViewer();
    rptViewer.ProcessingMode = ProcessingMode.Local;
    rptViewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + @"Reports\Report1.rdlc";
    rptViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", entities));
    ViewBag.ReportViewer = rptViewer;
    return View();
}

Note the repo object in the code above and the Report1DataSet. Your structuring might be different but I have a repository in the namespace Stackoverflow.web6.Data which returns an IEnumerable<Report1DataSet> (note that because we will need it later). The code above assumes you have the RDLC file Report1.rdlc inside your Reports folder in the root directory of the web app.

Now, in the Report1.rdlc report, when you add the dataset from Visual Studio, in the window you will have the option to select a data source from a drop down list, where the option Stackoverflow.web6.Data will be available and in the Available datasets field, all the methods in that repository, that return an IEnumerable<> can be selected. The dataset name must be "DataSet1" because that's what we are passing from the controller (see line 6 of my snippet above).

If you run the web app and navigate to the Report1 action controller, you will hopefully see the ReportViewer. You might need to install some more packages. Currently my working version of this has these packages.config definitions (related to reports) but you might not need all of them.

  <package id="MicosoftReportViewerWebForms_v11" version="1.0.1" targetFramework="net452" />
  <package id="Microsoft.Report.Viewer" version="11.0.0.0" targetFramework="net452" />
  <package id="Microsoft.ReportViewer" version="11.0.3366.16" targetFramework="net452" />
  <package id="Microsoft.ReportViewer.2012.Runtime" version="11.0.3452.0" targetFramework="net452" />
  <package id="Microsoft.ReportViewer.2015" version="12.0.2.2403" targetFramework="net452" />
  <package id="Microsoft.ReportViewer.2015.Runtime" version="12.0.2.2402" targetFramework="net452" />
  <package id="Microsoft.ReportViewer.Common" version="10.0.40219.1" targetFramework="net452" />
  <package id="Microsoft.ReportViewer.Runtime.Common" version="12.0.2402.15" targetFramework="net452" />
  <package id="Microsoft.ReportViewer.Runtime.WebForms" version="12.0.2402.15" targetFramework="net452" />
  <package id="Microsoft.ReportViewer.WebForms" version="10.0.40219.1" targetFramework="net452" />
  <package id="ReportViewerForMvc" version="1.0.1" targetFramework="net452" />
granit
  • 570
  • 4
  • 9
  • **granit**, Just to clear something at the Report Server Side, now that we have Dataset1 in MVC controller **rptViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", entities)); ViewBag.ReportViewer = rptViewer;** on the Report Server Side do I have to have the same dataset name as DataSet1 and the DataSource name should be same as **Stackoverflow.web6.Data** on the Report Server Side? And how/from where did you get the above window(Dataset Property) – Scavenger Apr 17 '17 at 06:10
  • @Scavenger actually if you are using ReportViewer like I did, you don't really need the Report Server. You only need the `.rdlc`. This will process reports in the ASP.NET application. In the case the reports are not local, you have to make some modifications in `rptViewer.LocalReport`. The dataset name must be the same and the window is opened when you want to create a new dataset in the RDLC report. – granit Apr 17 '17 at 06:30
  • **granit**, I am heavily depend on the Report Server and we have alot of reports on that server, but all using datasets from Report server itself, but here in C# I want to process the record set and send that to reports thats why I need the clear way to understand how to do that. Currently I am using MVC ReportViewer only inside the project. – Scavenger Apr 17 '17 at 07:50
  • **granit**, Should I update my question with the code that I am using? – Scavenger Apr 17 '17 at 08:53
  • @Scavenger I see. In principle it should be the same but the difference will be that that `ReportViewer` would use a `RemoteReport`. I will try to test it with SSRS as soon as I can. Posting the code you are using will help. – granit Apr 17 '17 at 09:09
  • **granit**, Just check above for updated code. I am simply passing parameters to the remote report server. But In my controller I also want to provide the processed dataset. – Scavenger Apr 17 '17 at 10:27
  • **granit**, Did you check my code and found the solution? – Scavenger Apr 18 '17 at 04:45
  • @Scavenger I checked it and read a little bit on the topic but apparently it cannot be done in this way because you can't _send_ the data set to the report if it's in the Report Server. My solution works only if you have the `.rdlc` file locally and everything is executed locally (in the web application). One way to do it maybe would be [via an XML web service data source](https://technet.microsoft.com/en-us/library/aa964129(v=sql.90).aspx) which get the data from a web service you can create in your application – granit Apr 18 '17 at 06:55
  • Can any one resolve my issue mentioned above. Thanks – Scavenger Apr 19 '17 at 12:30
  • MVC5-Visual S 2015 (HtmlHeler does not contan a definition for 'ReportView' and no extension method ) – SAR Nov 13 '17 at 10:44
0
    public ActionResult PrintReport(int? Id)
    {
        Profile profile = db.MyTable.Where(p => p.Id == Id).FirstOrDefault();
        IList<Profile> reportlist = new List<Profile>();
        reportlist.Add(profile);


        LocalReport localReport = new LocalReport();

        localReport.ReportPath = Server.MapPath("~/ReportTemplates/PrintContract.rdlc");

        ReportDataSource reportDataSource = new ReportDataSource("DataSet1", reportlist);

        localReport.DataSources.Add(reportDataSource);
        string reportType = "PDF";//Excel
        string mimeType;
        string encoding;
        string fileNameExtension;


        string deviceInfo =
        "<DeviceInfo>" +
        "  <OutputFormat>PDF</OutputFormat>" +
            //"  <OutputFormat>Excel</OutputFormat>" +
        "  <PageWidth>10in</PageWidth>" +
        "  <PageHeight>11in</PageHeight>" +
        "  <MarginTop>0.5in</MarginTop>" +
        "  <MarginLeft>.4in</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);

        return File(renderedBytes, mimeType);

    }
Muhammad Amin
  • 1,173
  • 8
  • 11