iT邦幫忙

2

[ASPX]ReportViewer連到伺服器報表,並給報表參數

  • 分享至 

  • xImage
  •  

用reportview call 伺服器報表,並給與參數

建立報表並上傳到 ReportServer:

使用 SQL Server Reporting Services (SSRS) 設計報表,並確保報表中的參數設置為隱藏。
將設計好的報表上傳到 ReportServer。
https://ithelp.ithome.com.tw/upload/images/20250207/20106764Lv1hJ4tuho.png

在 ASP.NET 頁面中使用 ReportViewer 控制項:

在您的 ASP.NET 專案中,新增一個 .aspx 頁面。
在該頁面中,添加 ReportViewer 控制項
aspx

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="1385px" Visible="False" ></rsweb:ReportViewer>

https://ithelp.ithome.com.tw/upload/images/20250207/201067642MTPncY3UH.png

aspx.cs


            string reportServerIp = "http://192.168.1.1/ReportServer";
            //指定使用伺服器
            ReportViewer1.ProcessingMode = ProcessingMode.Remote;
            //指定伺服器
            ReportViewer1.ServerReport.ReportServerUrl = new Uri(reportServerIp);
            //指定報表
            ReportViewer1.ServerReport.ReportPath = "/foldername/reportname";
            
            //
            //ReportViewer1.ServerReport.ReportServerCredentials = new ReportServerCredentials("your_username", "your_password", "your_domain");

            // 設定報表參數
            ReportParameter[] reportParameters = new ReportParameter[2]; 
            reportParameters[0] = new ReportParameter("YMD_S", txtYMD_S.Text);
            reportParameters[1] = new ReportParameter("YMD_E",txtYMD_E.Text); 
            ReportViewer1.ServerReport.SetParameters(reportParameters);
            ReportViewer1.ServerReport.Refresh();
            ReportViewer1.Visible = true;

目前這樣的code在iis express上面是OK的.
但上到IIS就有錯誤訊息,無法使用.(還不知道怎麼解決)

報表檢視器組態錯誤
報表檢視器 Web 控制項 HTTP 處理常式尚未在應用程式的 web.config 檔中註冊。請將 <add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> 加入 web.config 檔的 system.web/httpHandlers 區段,或將 <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" /> 加入 Internet Information Services 7 (含) 以後版本的 system.webServer/handlers 工作階段。

後來IIS網站換了pool就好了.看來是原pool不知哪裡有問題
https://ithelp.ithome.com.tw/upload/images/20250328/20106764n1kKpkLZLB.png

但換了pool,出現另一個問題
https://ithelp.ithome.com.tw/upload/images/20250328/20106764LwFRzeGa6j.png

處理方式,設定報表伺服器憑證
ReportViewer1.ServerReport.ReportServerCredentials = new ReportServerCredentials(username, password, domain);

[Serializable]
public class ReportServerCredentials : IReportServerCredentials
   {
       private string _username;
       private string _password;
       private string _domain;

       public ReportServerCredentials(string username, string password, string domain)
       {
           _username = username;
           _password = password;
           _domain = domain;
       }

       public WindowsIdentity ImpersonationUser
       {
           get { return null; }
       }

       public ICredentials NetworkCredentials
       {
           get { return new NetworkCredential(_username, _password, _domain); }
       }

       public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
       {
           authCookie = null;
           user = password = authority = null;
           return false;
       }
   }

這樣就可以了!!
如果不想 [Serializable]
可以改成
ReportViewer1.ServerReport.ReportServerCredentials = ReportServerCredentialsProvider.GetCredentials();

public static class ReportServerCredentialsProvider
{
    public static IReportServerCredentials GetCredentials()
    {
        string username = "xxx";
        string password = "xxx";
        string domain = "xxx"; // 根據您的實際情況設定 
        return new ReportServerCredentials(username, password, domain);
    }
}

圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言