Crystal Reports и привязка данных во время выполнения
Я боролся с этим уже 4 дня. У меня есть очень очень простой отчет о кристаллах (я использую его только для подтверждения концепции). Отчет привязан к базе данных, и я отображаю только одно поле из одной таблицы в базе данных. Нет вложенных отчетов. Он был создан с помощью Crystal Reports 2008. Мне нужно отобразить этот отчет в моем веб-приложении .Net MVC, но мне нужно иметь возможность изменить информацию о подключении к базе данных, так как это приложение. будет использоваться против разных баз данных с идентичной структурой таблиц. Поэтому я создал стандартную веб-форму и перетащил туда 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, но поле данных, которое у меня есть в моем отчете, пустое. Ты видишь что-то не так?
Большое спасибо заранее
Сьюзен