【问题标题】:Crystal Reports and data Binding at run timeCrystal Reports 和数据在运行时绑定
【发布时间】:2012-04-19 10:06:02
【问题描述】:

我已经为此苦苦挣扎了 4 天。 我有一个非常简单的水晶报告(我只是将它用于概念证明)。该报表绑定到一个数据库,我只显示数据库中一个表中的一个字段。没有子报表。它是使用 Crystal Reports 2008 创建的。我需要在我的 .Net MVC Web 应用程序中显示此报表,但我需要能够更改自此应用程序以来的数据库连接信息。将用于具有相同表结构的不同数据库。 因此,我创建了一个标准的 Web 表单,并将 CrystalReportViewer 和 CrystalReportSource 拖到其中。

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        this.CrystalReportSource1.EnableCaching = false;
        this.CrystalReportSource1.ReportDocument.Load(@"C:\ReportName.rpt");

        //1)  I get the data connection variables from my app - this part works well  
              and is irrelevant in this case.

 //2) Once I have the data I need to apply it to the connection of the report
 ConnectionInfo crConnection = new ConnectionInfo();
 crConnection.UserID = userID;
 crConnection.ServerName = datasource;
 crConnection.DatabaseName = "";
 crConnection.Password = password;


 AssignConnectionInfo(CrystalReportSource1.ReportDocument,crConnection);

 CrystalReportSource1.ReportDocument.DataSourceConnections[0].SetConnection 
 (crConnection.ServerName, crConnection.DatabaseName, false);

CrystalReportSource1.ReportDocument.SetDatabaseLogon(crConnection.UserID, 
crConnection.Password, crConnection.ServerName, crConnection.DatabaseName);


CrystalReportViewer1.ReportSource = CrystalReportSource1.ReportDocument;
CrystalReportViewer1.RefreshReport();

 }//close the page load function

这是 AssignConnectionInfo 函数:

private void AssignConnectionInfo(ReportDocument document,ConnectionInfo crConnection)
    {
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
        {
            TableLogOnInfo logOnInfo = table.LogOnInfo;
            if (logOnInfo != null)
            {
                table.ApplyLogOnInfo(table.LogOnInfo);
                table.LogOnInfo.TableName = table.Name;
                table.LogOnInfo.ConnectionInfo.UserID = crConnection.UserID;
                table.LogOnInfo.ConnectionInfo.Password = crConnection.Password;
                table.LogOnInfo.ConnectionInfo.DatabaseName = crConnection.DatabaseName;
                table.LogOnInfo.ConnectionInfo.ServerName = crConnection.ServerName;

                CrystalReportViewer1.LogOnInfo.Add(table.LogOnInfo);

            }
        }



    }

所以发生的情况是页面加载和 Crystal 横幅、工具栏显示,但我的报告中的数据绑定字段为空白。 有什么不对的地方吗?

非常感谢提前

苏珊

【问题讨论】:

    标签: crystal-reports


    【解决方案1】:

    在设置ConnectionInfo 的属性后,修改AssignConnectionInfo() 以调用ApplyLogOnInfo(),如下所示:

    private void AssignConnectionInfo(ReportDocument document,ConnectionInfo crConnection)
    {
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in document.Database.Tables)
        {
            TableLogOnInfo logOnInfo = table.LogOnInfo;
            if (logOnInfo != null)
            {
                table.LogOnInfo.TableName = table.Name;
                table.LogOnInfo.ConnectionInfo.UserID = crConnection.UserID;
                table.LogOnInfo.ConnectionInfo.Password = crConnection.Password;
                table.LogOnInfo.ConnectionInfo.DatabaseName = crConnection.DatabaseName;
                table.LogOnInfo.ConnectionInfo.ServerName = crConnection.ServerName;
                table.ApplyLogOnInfo(table.LogOnInfo);
    
                CrystalReportViewer1.LogOnInfo.Add(table.LogOnInfo);
    
            }
        }
    }
    

    维奥拉!

    【讨论】:

      【解决方案2】:

      而不是加载报表源对象。我认为您唯一需要做的就是创建一个 ReportDocument 对象,加载报表,使用您拥有的函数设置连接信息,然后将报表传递给查看器。您应该可以像这样省略 ReportSource 部分:

      protected void Page_Load(object sender, EventArgs e)
      {
          ReportDocument doc = new ReportDocument();
          doc.Load(@"C:\ReportName.rpt");
      
          ConnectionInfo crConnection = new ConnectionInfo();
          crConnection.UserID = userID;
          crConnection.ServerName = datasource;
          crConnection.DatabaseName = "";
          crConnection.Password = password;
      
          AssignConnectionInfo(doc,crConnection);
      
          CrystalReportViewer1.ReportSource = doc;
      
       }//close the page load function
      

      AssignConnectionInfo 函数完成了您在其中的许多其他代码的工作。此外,您不需要以下代码:

      CrystalReportViewer1.RefreshReport();
      

      报告应该在页面加载时运行,这样只会导致报告再次运行。希望这可以帮助。

      【讨论】:

      • 非常感谢。实际上,不同之处在于将代码从 Load 函数转移到 Reportviewer 对象的 Init 函数。现在可以了。再次感谢所有帮助。
      猜你喜欢
      • 2016-02-26
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      相关资源
      最近更新 更多