c# – 如何在打开子报表时阻止crystal report viewer查询登录凭据

前端之家收集整理的这篇文章主要介绍了c# – 如何在打开子报表时阻止crystal report viewer查询登录凭据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前有一个嵌入视觉工作室网站的水晶报告.我部署网站并将其安装在IIS上,并提供指向用户链接,以便他们可以全局访问并查看此报告.这个系统一直很好用.

但是,当我嵌入具有子报告的水晶报告时,凭据不会自动传递到子报告.当我调试解决方案时,初始报告打开正常,当我单击项目打开子报告时,水晶报告查看器会要求我提供数据库登录凭据.

如何在代码自动传递这些凭据,以便用户在水晶报表查看器中查看时无需输入代码.

下面是我在default.aspx.cs页面中使用的代码.它包含连接字符串.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.sqlClient;

namespace CFIBInventory
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender,EventArgs e)
        {
            sqlConnection con = new sqlConnection("Data Source=HPL-WTS;Initial Catalog=Enterprise32;Persist Security Info=True;User ID=sa;Password=********");
            DataSet1 ds = new DataSet1();
            sqlDataAdapter adapter = new sqlDataAdapter("SELECT     dbo.Material.MaterialCode,dbo.Material.CategoryCode,dbo.Material.Description,dbo.MaterialOnHand.LocationCode,dbo.Material.ValuationMethod,dbo.MaterialOnHand.Quantity FROM         dbo.Material INNER JOIN                      dbo.MaterialOnHand ON dbo.Material.MaterialCode = dbo.MaterialOnHand.MaterialCode WHERE     (dbo.Material.CategoryCode = 'CFIB3') AND (dbo.Material.ValuationMethod = 1) AND (dbo.Material.InactiveFlag = 0)",con);

            adapter.Fill(ds.cfibInventory);
            CrystalReport1 report = new CrystalReport1();
            report.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = report;

            CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
        }
    }
}

水晶报表查看器通过以下代码嵌入到我的.aspx页面中:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="CFIBInventory._Default" %>

<%@ Register assembly="CrystalDecisions.Web,Version=13.0.2000.0,Culture=neutral,PublicKeyToken=692fbea5521e1304" namespace="CrystalDecisions.Web" tagprefix="CR" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<Meta http-equiv="X-UA-Compatible" content="IE=9"/>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <p>
    </p>
    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
        AutoDataBind="true" />
</asp:Content>

子报告的查询位于子报告的数据库专家中.

顺便说一句,数据库连接到Windows Server 2005计算机.我不相信它有公共访问设置,例如它自己的IIS.以前的报告没有子报告,安装在较新的2012 Server上,就像数据库所在的那样.不确定机器是否与crystal report viewer中的其他登录提示有关.

任何帮助都会很棒.先感谢您!

编辑:从Nimesh实施解决方案后

好吧,我添加了连接的连接类型,但是,visual studio强调“报告”说:在声明之前不能使用局部变量’report’.

foreach(report.Database.Tables中的CrystalDecisions.CrystalReports.Engine.Table CrTable)

foreach(report.Subreports中的ReportDocument子报告)

我注意到我在下面宣布报告为新的crystalreport1.所以我将该声明移到Nimesh代码块之上,并且’report’的红色下划线消失了,但随后所有6个’crtablelogoninfo’实例都以红色加下划线并出现错误:当前上下文中不存在名称’crtablelogoninfo’

任何进一步的帮助将不胜感激.

以下是我的代码现在的样子:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.sqlClient;
    using CrystalDecisions.Shared;
    using CrystalDecisions.CrystalReports.Engine;

    namespace CFIBInventory
    {
        public partial class _Default : System.Web.UI.Page
        {


            protected void Page_Load(object sender,EventArgs e)
            {



                //Nimesh code
                ConnectionInfo crConnectionInfo = new ConnectionInfo();
                crConnectionInfo.ServerName = "HPL-WTS";
                crConnectionInfo.DatabaseName = "Enterprise32";
                crConnectionInfo.UserID = "sa";
                crConnectionInfo.Password = "*********";

                foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in report.Database.Tables)
                {
                    crTablelogoninfo = CrTable.logonInfo;
                    crtablelogoninfo.ConnectionInfo = crConnectionInfo;
                    CrTable.ApplylogonInfo(crtablelogoninfo);
                }
                foreach (ReportDocument subreport in report.Subreports)
                {
                    foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in subreport.Database.Tables)
                    {
                        crtablelogoninfo = CrTable.logonInfo;
                        crtablelogoninfo.ConnectionInfo = crConnectionInfo;
                        CrTable.ApplylogonInfo(crtablelogoninfo);
                    }
                } // nimesh code end

                //Old connection string,i assume this shouldnt be here since nimesh code is for connecting
                //sqlConnection con = new sqlConnection("Data Source=HPL-WTS;Initial Catalog=Enterprise32;Persist Security Info=True;User ID=sa;Password=123qwerTy987");

                DataSet1 ds = new DataSet1();
                sqlDataAdapter adapter = new sqlDataAdapter("SELECT     dbo.Material.MaterialCode,con);

                adapter.Fill(ds.cfibInventory);

                CrystalReport1 report = new CrystalReport1();

                 // OLD CODE
                //report.SetDataSource(ds);
                CrystalReportViewer1.ReportSource = report;

                CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
            }
        }
    }

解决方法

问题出在Crystal Report登录信息中.在显示报告之前,您必须将登录信息设置为主报告及其子报告中包含的所有表.您正在使用Disconnected Datasource Report Show方法.因此您无需为报告文档提供登录信息.你的问题在这里.
report.SetDataSource(ds);

当您使用SetDataSource方法时,必须提供水晶报告文档中包含的所有表.在这里,您只在数据集中传递了一个表.您必须传递所有表,包括子报表.
我建议你使用子报表然后使用连接的数据源方法,如果断开数据源(report.SetDataSource()).在Connected数据源中,您必须在显示报告之前设置登录信息.

private void PrintReport()
    {

        ReportDocument report = new ReportDocument();
        report.Load("ReportPath");

        ConnectionInfo crConnectionInfo = new ConnectionInfo();
        crConnectionInfo.ServerName = "HPL-WTS";
        crConnectionInfo.DatabaseName = "Enterprise32";
        crConnectionInfo.UserID = "sa";
        crConnectionInfo.Password = "*********";
        TablelogonInfo crTablelogoninfo = new TablelogonInfo();

        foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in report.Database.Tables)
        {
            crTablelogoninfo = CrTable.logonInfo;
            crTablelogoninfo.ConnectionInfo = crConnectionInfo;
            CrTable.ApplylogonInfo(crTablelogoninfo);
        }
        foreach (ReportDocument subreport in report.Subreports)
        {
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in subreport.Database.Tables)
            {
                crTablelogoninfo = CrTable.logonInfo;
                crTablelogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplylogonInfo(crTablelogoninfo);
            }
        }                         

        CrystalReportViewer1.ReportSource = report;

        CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
    }

如果您想使用断开连接的数据源,即使您没有包含子报表,也可以使用单个命令而不是多个表.这样可以轻松实现更好的性能.因为你只需要将一个表传递给SetDataSource方法.但是,不要忘记在DataTable中设置tablename,否则将不会显示报告.

原文链接:https://www.f2er.com/csharp/100939.html

猜你在找的C#相关文章