Solved

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

Posted on 2013-11-29
5
371 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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