Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I would be grateful if you could help me to solve the problem. Hello I try to pass some value from one page to another but at the run time i get exception.

Here is my ASP code:

ASP.NET
Enter a value to post: 
  <asp:textbox id="TextBox1" runat="Server"> 
  </asp:textbox>  
<br />
<br /> 
  <asp:button id="Button1" text="Post back to this page" runat="Server">      
  </asp:button>  
<br />
<br />  
  <asp:button id="Button2" text="Post value to another page" 
  postbackurl="PostBackUrlPage2cs.aspx" runat="Server"> 
  </asp:button>


Here's the code-behind on the destination page:

C#
void Page_Load(object sender, System.EventArgs e)    
 {        
  string text;          
  // Get the value of TextBox1 from the page that          
  // posted to this page.         
  text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;          
  // Check for an empty string.         
 if (text != "")            
  PostedLabel.Text="The string posted from the previous page is "+text+".";       
 else             
  PostedLabel.Text = "An empty string was posted from the previous page.";     
 } 


I get this Exception:
HTML
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


I took the example from msdn:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx

Why do I get this exception?and how can I solve this problem?

Thank you in advance.
Posted
Comments
Mich_90 10-Jan-12 10:24am    
I get Exception in this row:
text=((TextBox)PreviousPage.FindControl("TextBox1")).Text;
Sergey Alexandrovich Kryukov 10-Jan-12 15:01pm    
Use Debugger, nothing else. The issue is one of the simplest to find and debug.
--SA

Oh, I see. The problem is that FindControl returns null. This is a big common mistake to use this method. Why? You have access to all your controls via the members which are explicitly declared.

You can have a misspelling or something, and you compiler cannot detect a problem.

—SA
 
Share this answer
 
Try to debug your code and see where the error occurs. This will help you narrow down the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900