Hello,
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