Solved

Getting null reference when referencing public variable from another page

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

15 Experts available now in Live!

Get 1:1 Help Now