?
Solved

Getting null reference when referencing public variable from another page

Posted on 2008-10-17
5
Medium Priority
?
463 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

719 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