2009-09-17 53 views
0

我一直在爲此掙扎了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旗幟,工具欄顯示,但數據綁定字段我在我的報告中是空白的。 你有沒有看錯?

非常感謝提前

蘇珊

回答

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

報告應該運行在頁面加載時因此這將只是導致報表運行第二次。希望這可以幫助。

+0

非常感謝。實際上,區別在於將代碼從Load函數傳遞到Reportviewer對象的Init函數。它現在有效。再次感謝所有的幫助。 – suzi167 2009-09-20 03:04:15

3

修改AssignConnectionInfo()調用ApplyLogOnInfo()後,您的ConnectionInfo設置的屬性,就像這樣:

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

     } 
    } 
} 

中提琴!