reportdocument.setdatasource crystal报告中的nullreference错误

时间:2014-02-18 12:47:18

标签: c# asp.net crystal-reports

我在水晶报告中遇到问题rep_doc.SetDataSource(ds.Tables[0]);有错误我不知道代码有什么问题请提前帮助谢谢。 datable具有值但在CrystalReportViewer1.ReportSource = rep_doc;仍然显示空引用异常请提前帮助感谢。 第一部分是aspx.cs代码,第二部分是aspx代码

public void noOfEvents()
{
     dtEvents = objAdmin.GetNoOfEvents();
     if (dtEvents != null)
     {
        //Report Code
        DataSet ds = new DataSet();
        ds.Tables.Add(dtEvents);
        ReportDocument rep_doc = new ReportDocument();
        rep_doc.Load(Server.MapPath("../Reports/Events.rpt"));
        rep_doc.SetDataSource(ds.Tables[0]);
        CrystalReportViewer1.ReportSource = rep_doc;
        CrystalReportViewer1.DataBind();
        CrystalReportViewer1.RefreshReport();
      }
}


<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />

错误如下:

Line 42: rep_doc.SetDataSource(ds.Tables[0]);
Line 43:      
Line 44:CrystalReport1.ReportSource = rep_doc;
Line 45:CrystalReport1.DataBind();
Line 46:CrystalReport1.RefreshReport();



[NullReferenceException: Object reference not set to an instance of an object.]
   Assistvoter.Admin.Report.noOfEvents() in D:\ActiveProjects\Assist Voter\Assistvoter    \Admin\Report.aspx.cs:44
   Assistvoter.Admin.SuperAdmin.Button15_Click(Object sender, EventArgs e) in     D:\ActiveProjects\Assist Voter\Assistvoter\Admin\SuperAdmin.aspx.cs:95

System.Web.UI.WebControls.Button.OnClick(EventArgs e)+141    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)+149    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)+39    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+37        System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)+87        System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+4225

2 个答案:

答案 0 :(得分:0)

我猜你的CrystalReportViewer1为空。放置一个代码来检查null值,以避免使用对象的属性和方法(如果它为null)。

答案 1 :(得分:0)

此代码reportDocument.SetDataSource(dataset);遇到了相同的问题,但出现了异常Object reference not set to an instance of an object。经过数小时的尝试,我通过删除此

解决了问题

<dependentAssembly> <assemblyIdentity name="CrystalDecisions.ReportAppServer.DataSetConversion" publicKeyToken="692fbea5521e1304" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-13.0.3500.0" newVersion="13.0.3500.0" /> </dependentAssembly>

来自App.config,它起作用了。希望对您有所帮助。