Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 June 13th, 2010, 06:41 PM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Post Page 456 Step 16 - NullReferenceException

I can view NewPhotoAlbum.aspx in the browser. I enter a new album name "dog" and click "Insert". It never makes it to the ManagePhotoAlbum.aspx and I get the following error:

System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Web_yfg4uqyw"
StackTrace:
at NewPhotoAlbum.LinqDataSource1_Inserted(Object sender, LinqDataSourceStatusEventArgs e) in c:\Users\Mike\Desktop\WebSite3\NewPhotoAlbum.aspx. cs:line 17
at System.Web.UI.WebControls.LinqDataSourceView.OnIns erted(LinqDataSourceStatusEventArgs e)
at System.Web.UI.WebControls.LinqDataSourceView.Execu teInsert(IDictionary values)
at System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback)
InnerException:


Line 17 is :

Code:
    protected void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
        PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
        Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
            myPhotoAlbum.Id.ToString()));
    }
I don't see what I have done wrong. The code seems to be a copy of what I find in the book. Can anyone help ?
 
Old June 14th, 2010, 02:30 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Can you post the full code for the Markup and Code Behind sections? Please use the Remove Text Formatting buttons on the editor's toolbar when posting, and wrap the code using the Code tag (with the hash symbol) or the code is hard to read.

This can be either a problem in the code, or in the Linq to SQL model. Checking the code is the easiest to do, so let's try that first.

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!
 
Old June 14th, 2010, 11:50 AM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Post Page 456 Step 16 - NullReferenceException

Here is the code for the source (NewPhotoAlbum) and target (ManagePhotoAlbum) pages.

This is the code for the NewPhotoAlbum.aspx :

Code:
<%@ Page Title="Create New Photo Album" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" Inherits="NewPhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="Id" DataSourceID="LinqDataSource1" DefaultMode="Insert" 
        Height="50px" Width="125px">
        <Fields>
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
                ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:CommandField ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
        ContextTypeName="PlanetWroxDataContext" EnableInsert="True" 
        oninserted="LinqDataSource1_Inserted" OrderBy="Name" TableName="PhotoAlbums">
    </asp:LinqDataSource>
</asp:Content>

This is the code for the newPhotoAlbum.aspx.cs code behind :

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NewPhotoAlbum : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
        PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
        Response.Redirect(string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId={0}",
            myPhotoAlbum.Id.ToString()));
    }
}


And it is attempting to call ManagePhotoAlbum :

Code:
<%@ Page Title="Manage Photo Album" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ManagePhotoAlbum.aspx.cs" Inherits="ManagePhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" 
        DataSourceID="LinqDataSource1" InsertItemPosition="LastItem">
        <ItemTemplate>
            <li style="">
                Description:
                <asp:Label ID="DescriptionLabel" runat="server" 
                    Text='<%# Eval("Description") %>' />
                <br />
                Tooltip:
                <asp:Label ID="TooltipLabel" runat="server" Text='<%# Eval("Tooltip") %>' />
                <br />
                ImageUrl:
                <asp:Label ID="ImageUrlLabel" runat="server" Text='<%# Eval("ImageUrl") %>' />
                <br />
                
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
            </li>
        </ItemTemplate>
        
        
        <InsertItemTemplate>
            <li style="">
            Description:
                <asp:TextBox ID="DescriptionTextBox" runat="server" 
                    Text='<%# Bind("Description") %>' />
                <br />
                Tooltip:
                <asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
                <br />
                ImageUrl:
                <asp:TextBox ID="ImageUrlTextBox" runat="server" 
                    Text='<%# Bind("ImageUrl") %>' />
                <br />

                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                    Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Clear" />
            </li>
        </InsertItemTemplate>
        <LayoutTemplate>
            <ul class = "itemContainer" >
                <li ID="itemPlaceholder" runat="server" />
            </ul>
            </LayoutTemplate>
            
        </asp:ListView>
        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="PlanetWroxDataContext" EnableDelete="True" EnableInsert="True" 
            TableName="Pictures" Where="PhotoAlbumId == @PhotoAlbumId" 
            oninserting="LinqDataSource1_Inserting">
            <WhereParameters>
                <asp:QueryStringParameter DefaultValue="-1" Name="PhotoAlbumId" 
                    QueryStringField="PhotoAlbumId" Type="Int32" />
            </WhereParameters>
        </asp:LinqDataSource>
</asp:Content>

And its code behind page "ManagePhotoAlbum.aspx.cs" :

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ManagePhotoAlbum : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    {
        Picture myPicture = (Picture)e.NewObject;
        myPicture.PhotoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
    }
}
 
Old June 14th, 2010, 01:54 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I copied and pasted this code in my Planet Wrox project and it ran without an error.

Are you sure you set up the database correctly? Do both PhotoAlbum and Picture have an Id column which is the primary key and Identity for the table? And did you make them primary key and identity before you added them to the L2S diagram?

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!
 
Old June 14th, 2010, 11:14 PM
Registered User
 
Join Date: Jun 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Awesome. I revisited the tables and resaved the "PlanetWrox.mdf" file and everthing worked. Truthfully I didn't change any of the code so I don't really know what the root cause was.
Thanks Imar ! :)
 
Old November 9th, 2010, 03:02 AM
Registered User
 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default experiencing same problem on page 456

Hey imar
im new to the site so forgive me for interrupting this mans post but i do not know how to post a new thread.

I have followed your book and been successful in building the PlanetWrox website but i have reached a stand still on page 456 step 16. I created the LinqDataSource1 and added the photoalbum and picture tables to the LINQ to SQL Classes file and established the primary kesy in both tables creatinjg the relationship. When i try to request the NewPhotoAlbum.aspx in my browser but it gives me the following error

Quote:
Error 1 'System.Web.UI.WebControls.LinqDataSourceStatusEve ntArgs' does not contain a definition for 'NewObject' and no extension method 'NewObject' accepting a first argument of type 'System.Web.UI.WebControls.LinqDataSourceStatusEve ntArgs' could be found (are you missing a using directive or an assembly reference?) C:\BegASPNET\Site1\ManagePhotoAlbum.aspx.cs 16 40 C:\BegASPNET\Site1\
i am a beginner when it comes to object oriented code and i do not understand why the e class gets an error when it tries to call NewObject? why did you use the method NewObject and it is not recognized by the LinqDataSourceStatusEventArgs? Please assist me in this error

Thank you
 
Old November 9th, 2010, 03:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

It's a bit difficult to say since you didn't post your code, but NewObject is a property of LinqDataSourceInsertEventArgs, and not of LinqDataSourceStatusEventArgs so maybe you're using it at a wrong location? E.g. not in the control's Inserting event but in a different one?

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!
 
Old November 9th, 2010, 06:22 AM
Registered User
 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi imar

this is part of my code for the managephotoalbum.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Template1 : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoalbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));
}
}

yet the compiler gives me the error mentioned in my earlier post
 
Old November 9th, 2010, 06:26 AM
Registered User
 
Join Date: Nov 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry i meant that is ALL the code not just part
 
Old November 9th, 2010, 06:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
yet the compiler gives me the error mentioned in my earlier post
That makes sense to me, as you're using an incorrect argument for LinqDataSource1_Inserted. In your code, you're using a LinqDataSourceStatusEventArgs while you should be using a LinqDataSourceInsertEventArgs.

If you set up the Inserted event using the Events tab of the Properties Grid, VWD inserts the following code for you:

Code:
 
protected void LinqDataSource1_Inserted(object sender, LinqDataSourceInsertEventArgs e)
{
}
See the difference?

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 page 156 Example Step 4 NoraBelle BOOK: Professional Microsoft SQL Server 2008 Integration Services ISBN: 978-0-470-24795-2 1 August 2nd, 2010 03:29 PM
SQL Error, p. 456, step 16 member4953 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 April 8th, 2009 01:19 PM
Chapter 16 Try It Out Page 498 Step 7 workib BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 1 October 9th, 2008 08:22 PM
Page 222 Step 5 Whitney Schopf BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 November 6th, 2006 06:43 AM
step of opening a web page somnath.kartic VS.NET 2002/2003 0 March 30th, 2006 03:24 AM





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