对象引用未设置为ReportViewer中对象的实例

时间:2015-10-26 16:20:56

标签: c# asp.net reportviewer telerik-reporting

我在页面上有一个报告,如果我走开20分钟,我会收到错误消息对象引用未设置为对象的实例

AMFM.ReportViewer.GenerateReport(DataTable dt) in c:\Users\gorella\Documents\Visual Studio 2013\Projects\Web_Applications\AMFM\AMFM\ReportViewer.aspx.cs:76

AMFM.ReportViewer.Page_Init(Object sender, EventArgs e) in c:\Users\gorella\Documents\Visual Studio 2013\Projects\Web_Applications\AMFM\AMFM\ReportViewer.aspx.cs:65

第一个问题:上面的路径甚至不是我运行的用户配置文件或路径。

第二个问题是当用户尝试刷新页面时如何摆脱此错误。

此代码是从TFS复制的。这是第65和76行的代码

private void Page_Init(object sender, EventArgs e) {
    DataTable dt = (DataTable)Session["dataset"];

    String query = (String)Session["query"];
    //System.Diagnostics.Debug.WriteLine("********************" + query);
    //System.Diagnostics.Debug.WriteLine("########################" + dt.Rows.Count.ToString());
    if (dt == null) {
        System.Diagnostics.Debug.WriteLine("THIS IS A TEST EVENT MESSAGE Response Header----------- THIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT"); ;
    }
    GenerateReport(dt);
}

protected void GenerateReport(DataTable dt) {
    // Get the dataset from the session state. Passed in from Reports.aspx.
    // This way the dataset is only generated once.
    //Report treport = new Report();
    // Get the report node from the session state. Passed in from Reports.aspx

    ReportTreeNode rn = (ReportTreeNode)Session["report"];

    System.Diagnostics.Debug.WriteLine(String.Format("### {0}", rn.Text));
    //Report r = new Report();
    System.Diagnostics.Debug.WriteLine(String.Format(" {0}",
        rn.treport.ToString())
    );

    rn.treport.ReportParameters["title"].Value = rn.Text + " Report";
    rn.treport.ReportParameters["title2"].Value = rn.title2;
    rn.treport.DataSource = dt;
    TReportViewer1.ReportSource = rn.treport;    
    //TReportViewer1.Report = rn.treport;
    // do a refresh if needed.
    //TReportViewer1.RefreshReport();
}

需要帮助排除故障。如果此异常出现重定向到主页

,我至少也想做到这一点

2 个答案:

答案 0 :(得分:0)

我不完全确定这一点,但这是我能想象的唯一一件事与您离开页面的时间有关,会话可能在服务器上过期。< / p>

如果您有Page_Init,则需要对其进行修改,以便检查该查询是否为空

private void Page_Init(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["dataset"];

        String query = ""
        if(Session["query"]!= null){
            query = (String)Session["query"];
        }
        if (dt == null)
        {
            System.Diagnostics.Debug.WriteLine("THIS IS A TEST EVENT MESSAGE Response Header----------- THIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT"); ;
        }
        GenerateReport(dt);
    }

然后,当您下次使用它时,您需要检查查询,以确保它不是空字符串。

如果失败了,请将visual studio调试器附加到它上面,让它显示空引用的来源。

我不建议将错误重定向到非信息性的主页,而这可能看起来更容易呈现它实际上只是一个需要解决的问题的肮脏解决方案,并且可能会使未来出现问题更难来修复创建雪球效果。

答案 1 :(得分:0)

由于会话超时(默认情况下为20分钟且会话变量丢失数据),可能会发生错误。

private void Page_Init(object sender, EventArgs e) {
    DataTable dt;

    String query = "";
    if (Session["query"] != null) {
        query = (String)Session["query"];
    }
    if (Session["dataset"] != null) {
        dt = (DataTable)Session["dataset"];
        GenerateReport(dt);
    }
    else {
        System.Diagnostics.Debug.WriteLine("THIS IS A TEST EVENT MESSAGE Response Header----------- THIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT MESSAGETHIS IS A TEST EVENT"); ;
    }
}

同样在GenerateReport方法中检查任何其他会话变量,如ReportTreeNode等。

protected void GenerateReport(DataTable dt) {
    ReportTreeNode rn;
    if (Session["report"] != null) {
        rn = (ReportTreeNode)Session["report"];
        rn.treport.ReportParameters["title"].Value = rn.Text + " Report";
        rn.treport.ReportParameters["title2"].Value = rn.title2;
        rn.treport.DataSource = dt;
        TReportViewer1.ReportSource = rn.treport;    
    }
}