Solved

C#.NET class available in some methods but not in others

Posted on 2013-11-29
5
353 Views
Last Modified: 2013-12-16
Here is the codebehind:

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

Open in new window


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?

Any help would be wonderful!
Thanks!
0
Comment
Question by:EchoBinary
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 39686111
Without knowing what the AstraFunctions() class is doing it is difficult to say, but you could try setting the gridview object value in the method.

            try
            {
                gridview vdaa.gv = (gridview)gvViewDetailedASTRAActivity;
                vdaa.gv.ID.ToString(); // DOES NOT work!
                //vdaa.gv_PageIndexChanging(sender, e); // this DOES NOT work!
            }
0
 

Author Comment

by:EchoBinary
ID: 39686129
It seems to me from experimenting that the vdaa object itself is what is not an instance of an object.
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 39686158
I am assuming that  vdaa.SetUp assigns values to the vdaa object. In the original page load, it is not being ran unless you are doing that somewhere else as well. If the gridview pageIndexChanging is performing a postBack, the value is not being set. Put a breakpoint in the page load event on line 11- SetVDAAPanelView(); // <----  THIS WORKS! and see if it hits when the pageIndexChanging is fired.
0
 

Author Comment

by:EchoBinary
ID: 39690043
I came up with an answer kinda on my own - it was to load the objects into the session in Page_Load() like so:

vdaa = Session["vdaa"] != null ? (AstraFunctions)Session["vdaa"] : new AstraFunctions();

Open in new window


then I just load them back from session elsewhere.
0
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 500 total points
ID: 39690599
I would change the not Postback or add the vdaa.SetDataSet() to else clause. Setting this variable in a session variable will drastically increase your viewState size.

You can set it that way, but there are some security concerns with session variables. You should read through this page.

http://msdn.microsoft.com/en-us/library/ms178581.ASPX
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now