Microsoft ReportViewer 控件類型版本兼容問題及解決方法


錯誤


Parser Error Message: The base class includes the field 'rvEquipment', but its type (Microsoft.Reporting.WebForms.ReportViewer) is not compatible with the type of control (Microsoft.Reporting.WebForms.ReportViewer).

 

添加引用,因為項目的.Net框架版本是4.0,因此ReportViewer的版本是9.0.

因為使用的開發工具是Visual Studio 2013,因此默認的.Net框架版本是4.5,因此默認的Report Viewer版本是11.0.

此時,程序運行時,會出現版本不兼容的問題。但在錯誤提示中,顯示的是類型不兼容,而它們的類型確實一樣的,這就是困擾所在。

解決方法是,在工具箱中添加.Net 4.0的11.0版本的ReportViewer,並使用該控件來顯示報表。

此后新建頁面,拖入9.0版本的ReportViewer,即可以查看報表。

但是,原來在4.5環境下創建的頁面,使用9.0的控件替換11.0的控件后,仍然提示錯誤。

原因在於,ReportViewer版本的屬性,不是在某個控件上指定的,而是在所在的頁面中指定的。因此,不能在一個頁面中存在兩個不同版本的ReportViewer。注冊的代碼如下:

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

移除該段代碼后,重新拖入9.0的控件進行注冊。

再次運行,錯誤提示如下:


Compiler Error Message: CS0433: The type 'Microsoft.Reporting.WebForms.LocalReport' exists in both 'c:\Windows\assembly\GAC_MSIL\Microsoft.ReportViewer.WebForms\11.0.0.0__89845dcd8080cc91\Microsoft.ReportViewer.WebForms.DLL' and 'c:\Windows\assembly\GAC_MSIL\Microsoft.ReportViewer.WebForms\9.0.0.0__b03f5f7f11d50a3a\Microsoft.ReportViewer.WebForms.dll'

原因在於配置信息中存在兩個版本的配置。因此需要刪除11.0的配置信息,並把相應的11.0的信息替換為9.0的信息(替換內容為Version和PublicKeyToken)。

web.config配置信息

<?xml version="1.0"?>
<!--
  有關如何配置 ASP.NET 應用程序的詳細信息,請訪問
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <httpHandlers>
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
        validate="false" />
    </httpHandlers>
    <compilation debug="true" targetFramework="4.0">
      <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" />
        <add assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="Microsoft.ReportViewer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
      </assemblies>
      <buildProviders>
        <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
      </buildProviders>
    </compilation>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </handlers>
  </system.webServer>
</configuration>

配置完成后運行,仍然出錯,此時問題已經不再是ReportViewer了。因為ReportViewer引用的報表以及數據源等,都存在版本兼容的問題,都需要修改。

綜述,使用Visual Studio創建Report Application,版本需要特別注意,需要正確選擇.Net框架版本,否則后續移植需要大量的工作。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM