Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting null reference when referencing public variable from another page

Posted on 2008-10-17
5
Medium Priority
?
464 Views
Last Modified: 2012-06-27
I keep getting an "Object Reference Not Set to an Instance of an Object" error when trying to reference a public variable from a previous page.  I have included my code.  This is urgent, so I appreciate your input!

The error occurs because PreviousPage.Control is considered null.  I followed the MSDN example and can't figure out why it doesn't work.  Thanks!

Source page:
 
 public String Control
        {
 
            set
            {
                if (value == null)
                {
                    value = "";
                }
                mControl = value;
               
            }
 
            get
            {
                return mControl;
            }
        }
 
 protected void btnAllocate_Click(object sender, EventArgs e)
        {
 
            Control = GridView1.SelectedValue.ToString();
          
            if (Control != "" && Control != null)
            {
                Response.Redirect("Allocation.aspx", false);
            }
         
 
        }
 
Destination Page:
 
<%@ PreviousPageType VirtualPath="~/SearchOrders.aspx" %> 
 
 protected void Page_Load(object sender, EventArgs e)
        {
           lblControlNumber.Text = PreviousPage.Control;
        }

Open in new window

0
Comment
Question by:jvalescu
5 Comments
 
LVL 23

Expert Comment

by:Kamaraj Subramanian
ID: 22745275
Use like this..


            if (Control != null && Control != "" )
            {
                Response.Redirect("Allocation.aspx", false);
            }
         
0
 

Author Comment

by:jvalescu
ID: 22745625
Same error.  Control is not null before it goes to the Allocation.aspx page.  The error is occuring here :  lblControlNumber.Text = PreviousPage.Control;
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22745693
what is that mControl? Do you have
private string mControl;
before you have that public property.

Another thing is, The PreviousPage can retain its Control (like TextBox, CheckBox...) not public variable. You can try store that public property in ViewState before you redirect to Allocation.aspx, and use server.Transfer instead of redirect to maintain server variables.

ViewState["control"] = anyvalue;
Server.Transfer("Allocation.aspx", false);

But I dont think ViewState would work either.

A better way is create a hiddenField
<asp:HiddenField ID="hfControl" runat="server" />

and in btAllocate_click:
hfControl.Value = anyvalue;
Server.Transfer("Allocation.aspx", false);

In your Allocation.aspx if you want to refer back to that value

((HiddenField)this.PreviousPage.FindControl("hfControl")).value;

I am not sure about the syntax, but you can try out these directions above,

Hope this helps
JINN


0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 2000 total points
ID: 22745720
Well another better method for your case is just using QueryString very simple and nice:

Source page:

protected void btnAllocate_Click(object sender, EventArgs e)
        {
 
            Control = GridView1.SelectedValue.ToString();
         
            if (Control != "" && Control != null)
            {
                Response.Redirect("Allocation.aspx?control=" + Control , false);
            }
         
 
        }

Allocation.aspx:
lblControlNumber.Text = Request.QueryString["control"];

We all use querystring for this kind of navigation

JINN





0
 

Expert Comment

by:giles99
ID: 23402799
to use page.previouspage property you need to user Server.Transfer("xxx.aspx", True) instead of response.redirect.  Using querystrings are ok if the data is not sensitive but then you need to validate querystrings on the target page which can be a pain.

Hope this helps.

Giles
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

971 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