Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET 4 : in C# and VB
|
BOOK: Professional ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Professional ASP.NET 4 in C# and VB by Bill Evjen, Scott Hanselman, Devin Rader; ISBN: 9780470502204
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2011, 05:19 AM
Authorized User
 
Join Date: Apr 2009
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default Listing 1-10 NullReferenceException was unhandled by

Hello All,

I am getting the error NullReferenceException was unhandled by user code on the Listing I.10.

The exception occurs on the TextBox1 object.

My code is as follows.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Listing1-10.aspx.cs" Inherits="WebApplication1.Listing1_10" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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");

Label1.Text = "Hello " + pp_TextBox1.Text + "<br />" + "Date Selected " +
pp_Calendar1.SelectedDate.ToShortDateString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
</body>
</html>

What is a null reference exception?

Why does it apply to the TextBox1 object?

Any help would be most appreciated.

Kind Regards

Thomas
 
Old February 14th, 2012, 05:29 PM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Default Previous Page

Hi there

I think you're running this page directly. the source of the NullException in your sample refers to PreviousPage. it does not contains a control named 'TextBox1' so >>> (TextBox)PreviousPage.FindControl("TextBox1"); <<< return null. if you write:
string msg = ((TextBox)PreviousPage.FindControl("TextBox1")).Te xt;
You'll got the error here. NullReferenceException
__________________
happy every time, happy every where

Reza Baiat

Last edited by irProject; February 14th, 2012 at 05:32 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
System.NullReferenceException was unhandled by user code - Chapter 8 dtmmaxx BOOK: Beginning ASP.NET 4 : in C# and VB 10 September 1st, 2011 05:51 PM
nullreferenceexception was unhandled by user code rnavin BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 June 11th, 2011 09:26 AM
Chapter 10, listing 10-10-app kiwibrit BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 2 August 18th, 2009 04:21 AM
Listing 8-10 and Listing 8-16. Asp.Net BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 1 February 6th, 2008 01:11 PM
Ch. 10 Directory listing superkooi BOOK: Beginning ASP 3.0 1 March 3rd, 2005 07:39 AM





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