我一直在爲此掙扎了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旗幟,工具欄顯示,但數據綁定字段我在我的報告中是空白的。 你有沒有看錯?
非常感謝提前
蘇珊
非常感謝。實際上,區別在於將代碼從Load函數傳遞到Reportviewer對象的Init函數。它現在有效。再次感謝所有的幫助。 – suzi167 2009-09-20 03:04:15