2

I have done reporting using report viewer in asp.net . Now i want to create it in MVC 3 also. As i am very new to MVC , expect guidance from you people. Thanks !!!

Sonam Mohite
  • 885
  • 4
  • 19
  • 51

1 Answers1

0

You need to separately populate a dataset at runtime and associate it with your report.

If you used the Report Wizard to initially create your report, it should have created a dataset definition you can use - in your case, StudentDataSource.xsd. If you open that file, you will see your query along with a TableAdapter for the query.

Here's an example based on the question Kevin referred to above ( How can I use a reportviewer control in an asp.net mvc 3 razor view? )

A StudentReport.rdlc report, with a default DataSet1 dataset and a generated StudentDataSet.xsd...

Here is the modified File() action method in PDFController:

   public FileResult File() {
        // Create a new dataset
        StudentDataSet ds = new StudentDataSet();

        // Create and fill the Student data table
        // using the Student table adapter

        StudentDataSetTableAdapters.StudentTableAdapter dta =
               new StudentDataSetTableAdapters.StudentTableAdapter();
        dta.Fill(ds.Student);

        // Create a new report datasource with 
        //      Name = the dataset name in the report,
        //      Value = the populated data table.

        ReportDataSource rds = new ReportDataSource();
        rds.Name = "DataSet1";
        rds.Value = ds.Student;

        ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
        rv.ProcessingMode = ProcessingMode.Local;
        rv.LocalReport.ReportPath = Server.MapPath("~/Reports/StudentReport.rdlc");

        // Add the new report datasource to the report.
        rv.LocalReport.DataSources.Add(rds);

        rv.LocalReport.Refresh();

        byte[] streamBytes = null;
        string mimeType = "";
        string encoding = "";
        string filenameExtension = "";
        string[] streamids = null;
        Warning[] warnings = null;

        streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

        return File(streamBytes, mimeType, "StudentReport.pdf");
    }

Also, note that if you use this same code in the ASPXView.aspx page from the previous question, you'll need to import the namespaces for both your MVC project and the data set table adapter you are using.

ASPXView.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0,
           Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<%@ Import Namespace="ProjectNamespace" %>
<%@ Import Namespace="ProjectNamespace.StudentDataSetTableAdapters" %>

<!DOCTYPE html>

<html>
<head id="Head1" runat="server">
    <title>ASPXView</title>
</head>
<body>
    <div>
        <script runat="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
              StudentDataSet ds = new StudentDataSet();

              StudentTableAdapter dta = new StudentTableAdapter();
              dta.Fill(ds.Student);

              ReportDataSource rds = new ReportDataSource();
              rds.Name = "DataSet1";
              rds.Value = ds.Student;
              ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/StudentReport.rdlc");
              ReportViewer1.LocalReport.DataSources.Add(rds);
              ReportViewer1.LocalReport.Refresh();
            }
        </script>
        <form id="Form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">          
        </asp:ScriptManager>
        <rsweb:reportviewer id="ReportViewer1" runat="server" height="500" width="500" AsyncRendering="false"></rsweb:reportviewer>
        </form>        
    </div>
</body>
</html>
Community
  • 1
  • 1
Eric Munn
  • 580
  • 2
  • 8