水晶报表和数据在运行时绑定 [英] Crystal Reports and data Binding at run time

查看:172
本文介绍了水晶报表和数据在运行时绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码:

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

// 1)我从我的应用程序获取数据连接变量 - 这部分工作很好
,在这种情况下是不相关的。

// 2)一旦我有数据,我需要应用它到报表的连接
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();

} //关闭页面加载函数

这是AssignConnectionInfo函数

  private void AssignConnectionInfo(ReportDocument document,ConnectionInfo crConnection)
{
foreach(CrystalDecisions.CrystalReports。 Engine.Table表中的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);

}
}



}

所以发生的事情是,页面加载和水晶横幅,工具栏显示,但我在我的报告中的数据绑定字段是空白。



Susan

解决方案

而不是加载报表源对象。我想你唯一需要做的是创建一个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;

} //关闭页面加载函数

AssignConnectionInfo函数工作的很多其他代码,你在那里。此外,您不应该需要以下代码:

  CrystalReportViewer1.RefreshReport(); 

报告应在页面加载时运行,这样报告才会第二次运行。希望这可以帮助。


I have been struggling with that for 4 days now. I have a very very simple crystal report(I am using it just for a proof of concept). The report is bound to a database and I display only one field from one table in the database.No subreports.It was created with Crystal Reports 2008. I need to display this report in my .Net MVC web app but I need to be able to change the database connection information since this app. will be used against different databases with identical table structure. So I created a standard web form and I dragged a CrystalReportViewer and CrystalReportSource to it.

This is my code:

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

This is the AssignConnectionInfo Function:

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

            }
        }



    }

so what's happening is that the page loads and the Crystal banner, toolbar displays but the databound field that I have in my report is blank. Do u see anything wrong?

Thanks very very much in advance

Susan

解决方案

Instead of loading a report source object. I think the only thing you need to do is create a ReportDocument object, load the report, set the connection info using the function that you have, and pass the viewer the report. You should be able to leave out the ReportSource part like this:

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

The AssignConnectionInfo function does the work of a lot of the other code that you had in there. Also, you shouldn't need the the following code:

CrystalReportViewer1.RefreshReport();

The report should run when the page loads so this will just cause the report to run a second time. Hope this helps.

这篇关于水晶报表和数据在运行时绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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