I ha开发者_开发技巧ve a Crystal report that displays client data according to a period or by specific client IDs. The report is composed by 2 subreports that, if run from Crystal Report (CR), work perfectly according to the parameter accepted in the proper CR dialog.
Running from web page the first report page is displayed properly in a ReportViewer object. Checking with SQL Profiler, the queries are executed properly with the right parameters, retunrning 1 record for the first subreport and 2 records for the second. However clicking on the report to switch to the next page a message appears: "Object reference not set to an instance of an object.".
Here the code for the ReportViewer:
_crystalReportViewer.DisplayGroupTree = false;
_crystalReportViewer.HasCrystalLogo = false;
_crystalReportViewer.HasDrillUpButton = false;
_crystalReportViewer.HasToggleGroupTreeButton = false;
_crystalReportViewer.HasViewList = false;
_crystalReportViewer.ReportSource = _myReportDocument;
[...]
protected void Page_UnLoad (object sender, EventArgs e)
{
if (_crystalReportViewer != null)
_crystalReportViewer.Dispose();
_crystalReportViewer = null;
}
Debugging the code everything seems fine. I guess the issue might be with the ReportViewer or CR itself, but I cannot find a way to solve it. Could you give me any advise? Thanks in advance!
I got that the problem was with the Page_UnLoad event: it was invoked each time the next/previous page button in Cristal Report was clicked, disposing the Viewer and causing the Object Reference exception.
I added a condition to check if a PostBack happens:
protected void Page_UnLoad (object sender, EventArgs e)
{
if (!IsPostBack)
{
if (_crystalReportViewer != null)
_crystalReportViewer.Dispose();
_crystalReportViewer = null;
if (_myReportDocument != null)
{
_myReportDocument.Close();
_myReportDocument.Dispose();
}
_myReportDocument = null;
GC.Collect();
}
}
Unfortunately a small problem still persist: on the TEST machine, everything works fine but once deployed on DEV machine (they are 2 different servers with apparently same settings), it is possible to view just the first 2 records of the report, then by clicking on "Next" button of the CR Viewer nothing happens. All other features like "Go TO" and "Print" from CR work fine. It is quite strange since the database settings are correct, the report and code are the same...any idea?
精彩评论