打开子报表时如何阻止水晶报表查看器询问登录凭据 [英] How to stop crystal report viewer from asking login credentials when opening subreport

查看:16
本文介绍了打开子报表时如何阻止水晶报表查看器询问登录凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在 Visual Studio 网站中嵌入了水晶报表.我部署网站并将其安装在 IIS 上,并向用户提供链接,以便他们可以在全球范围内访问并查看此报告.这个系统一直运行良好.

I currently have a crystal report embedded in a visual studio website. I deploy the website and install it on an IIS, and provide a link to the user so they can access globally and see this report. This system has been working great.

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

However, when I embedded a crystal report that has a sub report, the credentials are not being passed to the sub-report automatically. When i debug the solution, the initial report opens fine, when I click the item to open the sub-report, crystal report viewer asks me for database login credentials.

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

How can i pass those credentials automatically in the code so that users dont have to enter the code when viewing in the crystal report viewer.

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

Below is the code i use in my default.aspx.cs page. It contains the connection strings.

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 页面中:

The crystal report viewer is embedded into my .aspx page via the code below:

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

子报表的查询在子报表的数据库专家中.

The query for the sub report is in the sub-reports' database expert.

顺便说一句,数据库正在连接到一台 windows server 2005 机器.我不相信它有公共访问设置,比如它自己的 IIS.以前没有子报告的报告安装在较新的 2012 服务器上,因为这就是数据库所在的位置.不确定机器是否与水晶报表查看器中的额外登录提示有关.

By the way, the database is connecting to a windows server 2005 machine. I do not believe it has public access setup, such as its own IIS. The previous reports, that do not have subreports, are installed on a newer 2012 Server as thats where the database is. Not sure if the machine has anything to do with the additional login prompt in crystal report viewer.

任何帮助都会很棒.提前谢谢!

Any help will be great. Thank you in advance!

好吧,我添加了连接的连接类型,但是,Visual Studio 在报告"下划线说:在声明之前不能使用局部变量报告".

Well i added the connected connection type however, visual studio is underlining 'report' saying: Cannot use local variable 'report' before it is declared.

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

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

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

foreach (ReportDocument subreport in report.Subreports)

我注意到我稍后在下面将报告声明为新的水晶报告 1.因此,我将该声明移至 Nimesh 代码块上方,report"的红色下划线消失,但随后crtableLogoninfo"的所有 6 个实例都以红色下划线显示错误:当前上下文中不存在名称crtableLogoninfo"

I noticed i declare report later below as new crystalreport1. So I move that declaration above Nimesh code block and the red underline for 'report' goes away, but then all 6 instances of 'crtableLogoninfo' get underlined in red with the error: The name 'crtableLogoninfo' does not exist in the current context'

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

Any further help will be greatly appreciated.

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

Below is how my code looks now:

 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, 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();

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

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

推荐答案

问题出在水晶报表登录信息中.在显示报告之前,您必须为包含在主报告及其子报告中的所有表设置登录信息.您正在使用断开连接的数据源报告显示方法.因此您无需向报告文档提供登录信息.你的问题就在这里.

The problem is in Crystal Report login info. before showing the report you have to set login info to all tables which are included in main report and their sub reports. You are using Disconnected Datasource Report Show method. so you don't need to provide login info to report document. your problem is here.

report.SetDataSource(ds);

当您使用SetDataSource方法时,需要提供水晶报表文档中包含的所有表格.在这里,您只传递了数据集中的一个表.您必须通过所有表,包括子报表表.

如果您正在使用子报表,我建议您使用连接的数据源方法,而不是如果断开连接的数据源(report.SetDataSource()).在连接的数据源中,您必须在显示报告之前设置登录信息.

when you are using SetDataSource method it is necessary to provide all tables which are included in crystal report document. here, you have passed only a single table in dataset. you must pass all tables including subreport tables.

I am suggesting you if you are using subreport then use connected datasource method instead if disconnected datasource(report.SetDataSource()). in Connected datasource you have to set login info before showing the report.

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 中设置表名,否则报表将不会显示.

if you want to use disconnected datasource even you have not included subreport then you can use a single command instead of multiple tables. that will make easy and gives better performance. becuase you just need to pass only a single table into SetDataSource method. but, dont forget to set tablename in DataTable otherwise report will not be displayed.

这篇关于打开子报表时如何阻止水晶报表查看器询问登录凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆