Search code examples
c#asp.netreportviewertelerik-reporting

Object reference not set to an instance of an object in ReportViewer


I have report on page that if I walk away for 20 minutes I get the error message Object reference not set to an instance of an object

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

First problem: The path above is not even the user profile or path that I am running this run from.

Second problem is how to get rid of this error when the user tried to refresh the page.

This code was copied from a TFS. This is the code on line 65 and 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();
}

Need help troubleshooting. I at least want to make it too where if this exception comes up to redirect to the homepage


Solution

  • The error may be occurring due to session timeout which is by default 20 minutes and your session variable lose data.

    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"); ;
        }
    }
    

    Also in the GenerateReport method check for any other session variables like ReportTreeNode etc.

    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;    
        }
    }