Solved

Getting null reference when referencing public variable from another page

Posted on 2008-10-17
5
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

617 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