I use crystal report to implement reporting in my c# windows application.I create a form to show print preview of Report.I use following code to show preview:
private ReportDocument _reportDocument;
public CrystalReportPrintPreviewForm(ReportDocument reportDocument)
{
InitializeComponent();
_reportDocument = reportDocument;
}
private void CrystalReportPrintPreviewForm_Load(object sender, EventArgs e)
{
if(_reportDocument!=null)
crystalReportViewer1.ReportSource = _reportDocument;
}
And also i use following code to send ‘ReportDocument’ to this form and show it:
ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(Application.StartupPath + "\\Reports\\WorkGroupReport.rpt");
kargarBandarDataset.WorkGroup.DefaultView.RowFilter = workGroupBindingSource.Filter;
reportDocument.SetDataSource(kargarBandarDataset.WorkGroup.DefaultView.ToTable());
reportDocument.SetParameterValue("CurrentDate",shamsi.ShamsiDate());
CrystalReportPrintPreviewForm crystalReportPrintPreview = new CrystalReportPrintPreviewForm(reportDocument);
crystalReportPrintPreview.ShowDialog();
Sometimes I get NullReferenceException error message in the following line of Code:
crystalReportPrintPreview.ShowDialog();
How Can i solve this problem?
There is no exact answer of how to handle NullReferenceException. Make sure that you enabled “cathching” of thrown expections in VS (Debug->Exception mark Common Language Runtime) find the place where expection if thrown and try to undestand the reasons.
Also, try to get as much more information from .Message property of Exception.
Typically NullReference caused by some null arguments submitting to method or constructor of class. Try to debug and see what you passing to object that throws an exception;