Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9
This is the forum to discuss the Wrox book Professional ASP.NET 3.5: In C# and VB by Bill Evjen, Scott Hanselman, Devin Rader; ISBN: 9780470187579
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 6th, 2009, 04:43 AM
Authorized User
Points: 297, Level: 5
Points: 297, Level: 5 Points: 297, Level: 5 Points: 297, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2009
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cap 1 pg 33 Oject reference not set to an instance of an object

Hello All,

I am getting an error "Object reference not set to an instance of an object"

As far as I can see everything is declared as a variable but there seems to be something not right about how the two pages are interacting because there is no red underlined errors in Visual Web Developer shoping that it can't see that the variable has been declared.

The error refers to TextBox1.

My page1.aspx file looks like this

Code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server"> 
public TextBox pp_TextBox1
{
get
{
return TextBox1;
}
}
public Calendar pp_Calendar1
{
get
{
return Calendar1;
}
}
 
protected void Button1_Click (object sender, System.EventArgs e)
{
Label1.Text = "Hello " + TextBox1.Text + "<br />" +
"Date Selected: " + Calendar1.SelectedDate.ToShortDateString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>First Page</title>
</head>
<body>
<form id="form1" runat="server">
Enter your name:<br />
<asp:Textbox ID="TextBox1" Runat="server">
</asp:Textbox>
<p>
When do you want to fly?<br />
<asp:Calendar ID="Calendar1" Runat="server"></asp:Calendar></p>
<br />
<asp:Button ID="Button1" Runat="server" Text="Submit page to itself" 
OnClick="Button1_Click" />
<asp:Button ID="Button2" Runat="server" Text="Submit page to Page2.aspx"
PostBackUrl="~/Page2.aspx" />
<p>
<asp:Label ID="Label1" Runat="server"></asp:Label></p>
</form>
</body>
</html>
My page2.aspx looks like this.

Code:
<%@ Page Language="C#" %>
<%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server"> 
protected void Page_Load(object sender, System.EventArgs e)
{
TextBox pp_Textbox1;
Calendar pp_Calendar1;
pp_Textbox1 = (TextBox)PreviousPage.FindControl("TextBox1");
pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Label1.Text = "Hello " + PreviousPage.pp_TextBox1.Text + "<br />" + "Date Selected: " +
PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
else
{
Response.Redirect("Page1.aspx");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Second Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
</html>
My Page2.cs file looks like this



Help....

Cheers

Tomche
  #2 (permalink)  
Old September 6th, 2009, 07:39 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
My Page2.cs file looks like this
That's not a lot.... ;-)

Your other code looks fine, so maybe the problem is in that file?

However, Page2 looks like a file with in-line code, so how / where do you have this code file exactly?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; September 7th, 2009 at 03:07 PM..
  #3 (permalink)  
Old September 9th, 2009, 09:04 AM
Authorized User
Points: 297, Level: 5
Points: 297, Level: 5 Points: 297, Level: 5 Points: 297, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2009
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

I reckon the problem here is that I am trying to open Page2.aspx.

This code is only meant to reference the previous page so if you open it directly in Internet Explorer it doesn't have a previous page to reference therefore it produces an object reference error because there is no previous page for an object.

Doesn't one have to open Page1.aspx first?

Does this sound logical???
Cheers

Tomche
  #4 (permalink)  
Old September 9th, 2009, 09:45 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Take another look at this:
Code:
 
TextBox pp_Textbox1;
Calendar pp_Calendar1;
pp_Textbox1 = (TextBox)PreviousPage.FindControl("TextBox1");
pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
  Label1.Text = "Hello " + PreviousPage.pp_TextBox1.Text + "<br />" + "Date Selected: " 
   +   PreviousPage.pp_Calendar1.SelectedDate.ToShortDateString();
}
else
{
  Response.Redirect("Page1.aspx");
}
You are protecting yourself from directly requesting Page2.apsx. If PreviousPage is null you're redirecting to Page1.aspx. However, before you do the null check, you're already trying to access the PreviousPage instance with this code:

Code:
pp_Textbox1 = (TextBox)PreviousPage.FindControl("TextBox1");
pp_Calendar1 = (Calendar)PreviousPage.FindControl("Calendar1");
Simply move that to inside the if check and you should be fine...

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #5 (permalink)  
Old December 1st, 2009, 02:58 AM
Friend of Wrox
Points: 742, Level: 10
Points: 742, Level: 10 Points: 742, Level: 10 Points: 742, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Location: Tehran, Iran
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default

because in Page2.aspx you only need the Text propery of TextBox1 you can use this code instead to increase performance:
string txt = ((TextBox)PreviousPage.FindControl("TextBox1")).Te xt;
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 13 pg448: Object reference not set to an instance of an object tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 15 August 8th, 2009 06:14 AM
Error - Object reference not set to an instance ? venkikrao VB.NET 2 July 5th, 2007 01:00 AM
Object reference not set to an instance of an obje hertendreef ASP.NET 2.0 Basics 3 July 1st, 2007 02:16 PM
Object Reference is Not Set to an Instance of an O blackinwhite General .NET 4 February 1st, 2005 05:42 AM
Object reference not set to an instance... steelman BOOK: ASP.NET Website Programming Problem-Design-Solution 6 May 7th, 2004 02:03 AM



All times are GMT -4. The time now is 03:03 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.