Microsoft Report 向导无法集成到 ASP.NET mvc 项目中

Microsoft Report wizard cannot integrate in ASP.NET mvc project

我按照 this tutorial 使用 C#

在 ASP.Net 中创建了 RDLC 报告

在我的例子中,我想在报告向导中生成与上面完全相同的报告example.then生成报告以不同的扩展名。

ReportViewer1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportViewer1.aspx.cs" Inherits="Project_name.Report.ReportViewer1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="height: 170px">
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <rsweb:reportviewer id="ReportViewer1" runat="server" width="600"></rsweb:reportviewer>
    </div>
    </form>
</body>
</html>

ReportViewer1.aspx.cs

using System;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
using System.Data;
using Project_name.Report;


namespace Project_name.Report
{
    public partial class ReportViewer1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
                DataSet dsCustomers = GetData("select top 20 * from AB_Products");
                ReportDataSource datasource = new ReportDataSource("Project_nameDataSet", dsCustomers.Tables[0]);
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(datasource);
            }
        }

        private DataSet GetData(string query)
        {
            string conString = ConfigurationManager.ConnectionStrings["Project_nameConnectionString"].ConnectionString;
            SqlCommand cmd = new SqlCommand(query);
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;

                    sda.SelectCommand = cmd;
                    using (DataSet dsCustomers = new DataSet())
                    {
                        sda.Fill(dsCustomers, "DataTable1");
                        return dsCustomers;
                    }
                }

            }
        }
    }
}

在我的例子中,我在 ReportViewer1.aspx.cs 文件

的后续行中得到红色波浪线
   1. ReportViewer1.ProcessingMode = ProcessingMode.Local;
   2. ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
   3. ReportViewer1.LocalReport.DataSources.Clear();
   4. ReportViewer1.LocalReport.DataSources.Add(datasource);

当我将鼠标悬停在上方时,它分别显示以下错误

1.'ReportViewer1' does not contain definition for 'ProcessingMode'
2.'ReportViewer1' does not contain definition for 'LocalReport'
3.'ReportViewer1' does not contain definition for 'LocalReport'
4.'ReportViewer1' does not contain definition for 'LocalReport'

这是我的文件夹层次结构

我应该怎么做才能完成这件事。这个问题是由于连接字符串错误导致的吗?还是由于引用了错误的数据资源?

问题是您的 web.config 文件不包含 ReportViewer 控件所需的程序集引用。

<system.web>
    <compilation debug="true" targetFramework="4.5.1">
      <assemblies>
        <add assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"/>
        <add assembly="Microsoft.ReportViewer.Common, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845DCD8080CC91"/>
        <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
      </assemblies>
    </compilation>
</system.web>

而且您很可能还缺少 ReportViewer1.aspx.designer.cs 文件中的控件声明。

public partial class ReportViewer1 {

    /// <summary>
    /// form1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.HtmlControls.HtmlForm form1;

    /// <summary>
    /// ReportViewer1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::Microsoft.Reporting.WebForms.ReportViewer ReportViewer1;
}

但最简单的做法是首先按照正确的方法创建 WebForm,然后 Visual Studio 将为您添加引用。首先,将 Web 表单添加到您的 MVC 项目。

  1. 在解决方案资源管理器中右键单击该项目,然后转到“添加”>“Web 窗体”。
  2. 为表单指定一个描述性名称。

然后添加ReportViewer控件。

  1. 单击工具箱(通常在 Visual Studio 的左侧)。
  2. 打开 Reporting 扩展部分。
  3. 将 ReportViewer 拖放到页面上的 <form> 标记中。

您将得到一个如下所示的表格:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MvcApplication11.WebForm1" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server"></rsweb:ReportViewer>
    </div>
    </form>
</body>
</html>

然后您可以将代码添加到代码隐藏文件(在本例中为 WebForm1.aspx.cs)。您将能够访问位于 /WebForm1.aspx 的页面,但如果您喜欢使用 .NET 路由,则可以更改 URL。

参考:https://msdn.microsoft.com/en-us/library/aa337091.aspx