Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2012, 06:08 PM
Authorized User
Points: 57, Level: 1
Points: 57, Level: 1 Points: 57, Level: 1 Points: 57, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default Chapter 15 "Using the Cache API" Server Error Message

I am doing the last try it out in chapter 15, "Using the cache API" I have A problem with the Title. the book tells me to remove the title attribute completely from the page directive because I am going to set it Programmatically so i do and carefully follow every step through the try it out and then at then end i get an error saying "Object reference is set to an instance of an object" but if i give the ViewDetails.aspx page a title it works just fine. I don't know what I'm missing though so hope yall can. here is the code behind and html for All.aspx and ViewDetails.aspx.

(ViewDetails.aspx HTML Beginning)

<%@ Page Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="ViewDetails.aspx.vb" Inherits="Reviews_ViewDetails" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<h1><asp:Label ID="TitleLabel" runat="server"></asp:Label></h1>
<asp:Label ID="SummaryLabel" runat="server" CssClass="Summary"></asp:Label>
<asp:Label ID="BodyLabel" runat="server"></asp:Label>

</asp:Content>

(ViewDetails.aspx HTML ending)

(VeiwDetails.aspx Code behind)

Imports PlanetWroxModel

Partial Class Reviews_ViewDetails
Inherits BasePage
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim reviewId As Integer = Convert.ToInt32(Request.QueryString.Get("ReviewId" ))
Dim myReview As Review = TryCast(Cache("Reviews" + reviewId.ToString()), Review)
Label1.Text = "In the cache"
If myReview Is Nothing Then
Label1.Text = "NOT in the cache"
Using myEntities As New PlanetWroxEntities()
myReview = (From r In myEntities.Reviews
Where r.Id = reviewId
Select r).SingleOrDefault()
If myReview IsNot Nothing Then
Cache.Insert("Reviews" + reviewId.ToString(), myReview, Nothing,
DateTime.Now.AddMinutes(20),
System.Web.Caching.Cache.NoSlidingExpiration)
End If
End Using
End If
If myReview IsNot Nothing Then
TitleLabel.Text = myReview.Title
SummaryLabel.Text = myReview.Summary
BodyLabel.Text = myReview.Body
Title = myReview.Title
MetaDescription = myReview.Summary
End If
End Sub
End Class
(ViewDetails.aspx code behind ending)

(All.aspx HTML Beginning)
<%@ Page Title="All Reviews" Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="All.aspx.vb" Inherits="Reveiws_All" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# "ViewDetails.aspx?ReviewId=" + Eval("Id").ToString() %>'
Text='<%# Eval ("Title") %>'></asp:HyperLink>
<br />
</ItemTemplate>
</asp:Repeater>
</asp:Content>

(All.aspx HTML ending)

(All.aspx Code Behind beginning)

Imports PlanetWroxModel
Partial Class Reveiws_All
Inherits BasePage

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Using myEntities As New PlanetWroxEntities()
Dim allReviews = From review In myEntities.Reviews
Where review.Authorized = True
Order By review.CreateDateTime Descending
Select review
Repeater1.DataSource = allReviews
Repeater1.DataBind()
End Using
End Sub
End Class

(All.aspx Code behind ending)

Hope yall can help!
Reply With Quote
  #2 (permalink)  
Old August 8th, 2012, 02:45 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

I just copied and pasted your code into my project and it works fine for me. Maybe for some reason the review isn't correctly queried from the database (although I wouldn't know why).

Could you post more information about the object reference error? It's usually followed by the stack trace which may reveal where it crashes.

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!
Reply With Quote
  #3 (permalink)  
Old August 8th, 2012, 03:22 PM
Authorized User
Points: 57, Level: 1
Points: 57, Level: 1 Points: 57, Level: 1 Points: 57, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default Stack Trace.

WHAT? That's weird. well anyway. here's the stack trace.



[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.Page.get_Title() +71
BasePage.Page_PreRender(Object sender, EventArgs e) in C:\BegASPNET\Site\App_Code\BasePage.vb:12
System.Web.UI.Page.OnPreInit(EventArgs e) +9008326
System.Web.UI.Page.PerformPreInit() +31
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +328
Reply With Quote
  #4 (permalink)  
Old August 8th, 2012, 03:28 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Which version of Visual Studio are you running? Check Help | About for the full details. Does it say RTM or SP1?

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!
Reply With Quote
  #5 (permalink)  
Old August 8th, 2012, 04:06 PM
Authorized User
Points: 57, Level: 1
Points: 57, Level: 1 Points: 57, Level: 1 Points: 57, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Default Which Version

I am running "Microsoft Visual Web Developer Express SP1Rel"
Reply With Quote
  #6 (permalink)  
Old August 8th, 2012, 04:10 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Then I don't really know what's going on ;-(

It works as advertised when I wrote the book, and it still works that way on all machines I just tested it on.

Do you have any versions of the beta of VS 2012 installed? (I have on some of my mechines and it still works fine so I don't think that's itl just trying to find a potential cause for this).

Also, could you send me your zipped project so I can take a look?

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!
Reply With Quote
  #7 (permalink)  
Old August 8th, 2012, 04:19 PM
Authorized User
Points: 57, Level: 1
Points: 57, Level: 1 Points: 57, Level: 1 Points: 57, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Red face

How do you send a file attachment?
Reply With Quote
  #8 (permalink)  
Old August 8th, 2012, 04:47 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I think only authors and Wrox employees can do that. But you find my e-mail address in the About the Author section on page 9.

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!
Reply With Quote
  #9 (permalink)  
Old August 9th, 2012, 02:37 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Received your file; thanks. I also found the problem. Take a look at the code in BasePage:

Code:
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
The method name contains PreRender, while the event you're handling is PreInit. The correct event to handle the title check is PreRender.

During PreInit (which fires very early in the page's life cycle) the title is not available yet and you get an error. Change PreInit to PreRender (which fires very late in the life cycle) and the error should go away.

You may also want to fix the spelling of Review in Web.sitemap (and other pages) so your All menu item works again.

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!
Reply With Quote
  #10 (permalink)  
Old August 10th, 2012, 03:11 PM
Authorized User
Points: 57, Level: 1
Points: 57, Level: 1 Points: 57, Level: 1 Points: 57, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Red face err its back...

I did what you said to do on your last post and now when I try to open a page it gives me this error.

The 'Theme' property can only be set in or before the 'Page_PreInit' event.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The 'Theme' property can only be set in or before the 'Page_PreInit' event.

Source Error:


Line 7: Dim PreferredTheme As HttpCookie = Request.Cookies.Get("PreferredTheme")
Line 8: If PreferredTheme IsNot Nothing Then
Line 9: Page.Theme = PreferredTheme.Value
Line 10:
Line 11: End If

Source File: C:\BegASPNET\Site\App_Code\BasePage.vb Line: 9

Stack Trace:


[InvalidOperationException: The 'Theme' property can only be set in or before the 'Page_PreInit' event.]
System.Web.UI.Page.set_Theme(String value) +2806587
BasePage.Page_PreRender(Object sender, EventArgs e) in C:\BegASPNET\Site\App_Code\BasePage.vb:9
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnPreRender(EventArgs e) +8996626
System.Web.UI.Control.PreRenderRecursiveInternal() +103
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

Please take all the time you need to wright back. I am taking WAY to much time from you.
Reply With Quote
Reply


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
Chapter 2:Dialog project "force close" error ichthus BOOK: Beginning Android Application Development 4 May 16th, 2011 11:16 AM
Chapter 15. "Sign Up for new account" gets error: 26 - Error Locating Server/Instance Tanya Roslyakova BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 March 8th, 2011 10:34 AM
MSXSL gives error message for "for" inside "select" ilyaz XSLT 1 December 9th, 2010 05:02 PM
Chapter 18: Getting "Failed to generate a user instance of SQL Server" error HungryCaterpillar BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 November 11th, 2009 01:56 PM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM



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


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