0

Here is the code behind:

public partial class ViewDetailedActivity : System.Web.UI.Page
{
    public AstraFunctions vdaa = new AstraFunctions();
    public AstraFunctions vdaabdt = new AstraFunctions();
    public AstraFunctions vdaabdc = new AstraFunctions();
    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            {
                AstraHdr.Set_Title("View Detailed ASTRA Activity");
                SetVDAAPanelView(); // <----  THIS WORKS!
            }
    }
/*.............*/
    protected void SetVDAAPanelView()
    {
        try
        {
//THIS ALL WORKS
            string sSqlCommand = "Astra_ReportActivity";
            SqlParameter[] SQLParameters = null;
            vdaa.SetUp(gvViewDetailedASTRAActivity, sSqlCommand, SQLParameters);
            vdaa.SetDataSet();
            Response.Write(vdaa.gv.ID.ToString();
            pnlViewDetailedASTRAActivity.Visible = true;
            pnlViewDetailedASTRAActivityByDate.Visible = false;
            pnlViewDetailedASTRAActivityByDODACC.Visible = false;
        }
        catch (Exception ex)
        {
            Response.Write("Error in SetVDAAPanelView():  " + ex);
        }
    }
/*..............*/
    protected void gvViewDetailedASTRAActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            vdaa.gv.ID.ToString(); // DOES NOT work!
            //vdaa.gv_PageIndexChanging(sender, e); // this DOES NOT work!
        }
        catch (Exception ex)
        {
            Response.Write("Error in gvViewUnprocessedReceipts_PageIndexChanging():  " + ex);
        }
    }
}

When I trigger gvViewDetailedASTRAActivity_PageIndexChanging I get the following error:

Error in gvViewDetailedASTRAActivity_PageIndexChanging(): System.NullReferenceException: Object reference not set to an instance of an object. at VIM.ASTRA.ViewDetailedASTRAActivity.gvViewDetailedASTRAActivity_PageIndexChanging(Object sender, GridViewPageEventArgs e) at [filename]: line [linenumber]

I can access the class object in SetVDAAPanelView() but not gvViewDetailedASTRAActivity_PageIndexChanging(object sender, GridViewPageEventArgs e) -- what gives?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
EchoBinary
  • 125
  • 1
  • 2
  • 9
  • When you debug, is vdaa.gv.ID null? or a valid value? – James Smith Nov 29 '13 at 21:38
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 29 '13 at 22:10

1 Answers1

0

Try to store you object in session

public AstraFunctions vdaa;
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
          vdaa =  Session["vdaa"] != null ? 
                  (AstraFunctions)Session["vdaa"] : new AstraFunctions();
          ...//do somthing with vdaa 
          Session["vdaa"] = vdaa;
        }
}


protected void gvViewDetailedASTRAActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
           AstraFunctions vdaa = (AstraFunctions)Session["vdaa"]  
            vdaa.gv.ID.ToString();
            vdaa.gv_PageIndexChanging(sender, e); 
        }
        catch (Exception ex)
        {
            Response.Write("Error in gvViewUnprocessedReceipts_PageIndexChanging():  " + ex);
        }
Ilya Sulimanov
  • 7,636
  • 6
  • 47
  • 68
  • a slight variation on this was the answer. it turns out that it wasn't the class itself that was null, but all the stuff inside the class - due to where/how I was doing the class "init" vs. the page refresh/postback. Storing it in the session solved my problem. – EchoBinary Nov 30 '13 at 03:38