Solved

Getting null reference when referencing public variable from another page

Posted on 2008-10-17
5
459 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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

808 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