Wednesday, April 5

Crystal reports and empty report

I've been trying to use Crystal Reports with Visual Studio 2005 and SQL Server 2005 Express. When I dragged the CrystalReportViewer control to a Windows Form and added a very simple report to this form, the report headings were displayed but no data. There were no error messages to explain why this was the case.

Reading around the net lead me to think this was likely to be with something to do with database security or firewall settings. However, after lots of trial and error I found that I had to do the following:-

1) Explicitly fill the datasets that were used in the report.
2) Go through each table in the report and set its data source.
3) Set the report source to the report with the modified tables.

The code I used was as follows:


private void MyForm_Load(object sender, EventArgs e)
{
this.myTableAdapter1.Fill(this.myDataSet1.
MYTABLE);
myReport crtp = new myReport();

foreach (CrystalDecisions.
CrystalReports.Engine.Table
tb in crtp.Database.Tables)
{
tb.SetDataSource(myDataSet1);
}

crystalReportViewer1.ReportSource = crtp;
}


This wasn't anything to do with security settings.

No comments: