简体   繁体   中英

Microsoft Report wizard cannot integrate in ASP.NET mvc project

I followed this tutorial to create RDLC report in ASP.Net using C#

In my case I want to generate report in Report Wizard exactly like above example.then generate to report in different extensions.

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;
                    }
                }

            }
        }
    }
}

In my case I'm getting red squiggly line in following lines in ReportViewer1.aspx.cs file

   1. ReportViewer1.ProcessingMode = ProcessingMode.Local;
   2. ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Report.rdlc");
   3. ReportViewer1.LocalReport.DataSources.Clear();
   4. ReportViewer1.LocalReport.DataSources.Add(datasource);

When I hover over on above line its showing me following errors respectively

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'

this is my folder hierarchy

在此处输入图片说明

What should I do to get this thing done. Is this problem occur due to wrong connectionstring ? or due to referring wrong data resource ?

The problem is that your web.config file doesn't contain the necessary assembly references for the ReportViewer control.

<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>

And you are most likely also missing the declaration of the control in your ReportViewer1.aspx.designer.cs file.

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;
}

But the simplest thing to do is to follow the right way to create a WebForm in the first place and then Visual Studio will add the references for you. First, add a web form to your MVC project.

  1. Right click the project in solution explorer and go to Add > Web Form.
  2. Give the form a descriptive name.

在此处输入图片说明

Then add the ReportViewer control.

  1. Click on the Toolbox (typically on the left side of Visual Studio).
  2. Open the Reporting expanding section.
  3. Drag and drop the ReportViewer into the <form> tag on the page.

在此处输入图片说明

You will then have a form that looks something like this:

<%@ 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>

And then you can add the code to the codebehind file (in this case it would be WebForm1.aspx.cs ). You will be able to access the page at /WebForm1.aspx , but you can change the URL if you like using .NET routing.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM