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: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 July 5th, 2009, 04:02 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 Chap 13 pg448: Object reference not set to an instance of an object

Hello All,

I get a object reference not set to an instance of an object error after completing the code up to page 448.

The Source Error is produced as follows.
Line 15: {Line 16: PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;Line 17: Response.Redirect(string.Format("ManagePhotoAlbum. aspx?PhotoAlbumId=(0)",Line 18: myPhotoAlbum.id.ToString()));Line 19: What could be wrong? Regards Thomas
Reply With Quote
  #2 (permalink)  
Old July 5th, 2009, 04:12 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Difficult to say with this tiny bit of code. Looks like e.Result is null but I am not sure.

Can you post more code and describe the steps you're taking? E.g. how do you access this page, whether or not you see the items end up in the database and so on.

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 July 5th, 2009, 04:29 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 object reference not set to instance of an object

HI Imar,
When I open up NewPhotoAlbum.aspx I get a box with two text boxes. The top one is labeled Id and the second one is labeled Name.

I tried entering no data into either box and this genrated the error

Then I tried entering the number 2 into id and then gibberish into the second and this also produced the error.

Below is the code in my pages

newphotoalbum.aspx
<%@PageTitle="Create New Photo Album"Language="C#"MasterPageFile="~/MasterPages/Masterpage.master"AutoEventWireup="true"CodeFile="NewPhotoAlbum.aspx.cs"Inherits="NewPhotoAlbum" %>
<asp:ContentID="Content1"ContentPlaceHolderID="head"Runat="Server">
</
asp:Content>
<
asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"Runat="Server">
<asp:DetailsViewID="DetailsView1"runat="server"AutoGenerateRows="False"DataKeyNames="id"DataSourceID="LinqDataSource1"DefaultMode="Insert"Height="50px"Width="125px">
<Fields>
<asp:BoundFieldDataField="id"HeaderText="id"ReadOnly="True"SortExpression="id"/>
<asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name"/>
<asp:CommandFieldShowInsertButton="True"/>
</Fields>
</asp:DetailsView>
<asp:LinqDataSourceID="LinqDataSource1"runat="server"ContextTypeName="PlanetWroxDataContext"EnableInsert="True"oninserted="LinqDataSource1_Inserted"TableName="PhotoAlbums">
</asp:LinqDataSource>
<p>
</p>
</
asp:Content>



newphotoalbum.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
publicpartialclassNewPhotoAlbum : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid LinqDataSource1_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
Response.Redirect(
string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId=(0)",
myPhotoAlbum.id.ToString()));
}
}




ManagePhotoAlbum.aspx
<%@PageTitle="Manage Photo Album"Language="C#"MasterPageFile="~/MasterPages/MasterPage.master"AutoEventWireup="true"CodeFile="ManagePhotoAlbum.aspx.cs"Inherits="ManagePhotoAlbum" %>
<asp:ContentID="Content1"ContentPlaceHolderID="head"Runat="Server">
</
asp:Content>
<
asp:ContentID="Content2"ContentPlaceHolderID="cpMainContent"Runat="Server">
<asp:ListViewID="ListView1"runat="server"DataKeyNames="Id"DataSourceID="LinqDataSource1"InsertItemPosition="LastItem">
<LayoutTemplate>
<ulclass="itemContainer">
<liID="itemPlaceholder"runat="server"/>
</ul>
</LayoutTemplate>
<InsertItemTemplate>
<listyle="">
Description:
<asp:TextBoxID="DescriptionTextBox"runat="server"Text='<%# Bind("Description") %>'/>
<br/>
Tooltip:
<asp:TextBoxID="TooltipTextBox"runat="server"Text='<%# Bind("Tooltip") %>'/>
<br/>
ImageUrl:
<asp:TextBoxID="ImageUrlTextBox"runat="server"Text='<%# Bind("ImageUrl") %>'/>
<br/>
PhotoAlbumId:
<asp:TextBoxID="PhotoAlbumIdTextBox"runat="server"Text='<%# Bind("PhotoAlbumId") %>'/>
<br/>
PhotoAlbum:
<asp:TextBoxID="PhotoAlbumTextBox"runat="server"Text='<%# Bind("PhotoAlbum") %>'/>
<br/>
<asp:ButtonID="InsertButton"runat="server"CommandName="Insert"Text="Insert"/>
<asp:ButtonID="CancelButton"runat="server"CommandName="Cancel"Text="Clear"/>
</li>
</InsertItemTemplate>
<ItemTemplate>
<listyle="">

Description:
<asp:LabelID="DescriptionLabel"runat="server"Text='<%# Eval("Description") %>'/>
<br/>
Tooltip:
<asp:LabelID="TooltipLabel"runat="server"Text='<%# Eval("Tooltip") %>'/>
<br/>
ImageUrl:
<asp:LabelID="ImageUrlLabel"runat="server"Text='<%# Eval("ImageUrl") %>'/>
<br/>
<asp:ButtonID="DeleteButton"runat="server"CommandName="Delete"Text="Delete"/>
</li>
</ItemTemplate>
</asp:ListView>
<asp:LinqDataSourceID="LinqDataSource1"runat="server"ContextTypeName="PlanetWroxDataContext"EnableDelete="True"EnableInsert="True"TableName="Pictures"Where="PhotoAlbumId == @PhotoAlbumId"oninserting="LinqDataSource1_Inserting">
<WhereParameters>
<asp:QueryStringParameterDefaultValue="-1"Name="PhotoAlbumId"QueryStringField="PhotoAlbumId"Type="Int32"/>
</WhereParameters>
</asp:LinqDataSource>
</
asp:Content>

managephotoalbum.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
publicpartialclassManagePhotoAlbum : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
Picture myPicture = (Picture)e.NewObject;
myPicture.PhotoAlbumId =
Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"));
}
}


Hope this helps

Regards

Thomas
Reply With Quote
  #4 (permalink)  
Old July 5th, 2009, 04:41 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

A couple of things:

1. Please use the "Remove Text Formatting" button on the toolbar of the editor when posting here. Then wrap the text in code tags by using the code button. Otherwise, the code gets impossible to read.

2. Please provide more information. Like I said in my previous message, I need to know things like whether or not the items shows up in the database or not. Because of a bug in this forum I cannot use your code as-is (or at all, in fact), so it's difficult for me to reproduce the problem without more background information.

Then make sure you did the following:

1. Make Id a primary key in step 1
2. Enable inserting on the data source in step 6
3. Enable inserting on the FormView in step 7
4. Set the default mode to Insert.

If you confirmed all this and it still doesn't work, take a copy of this page from the download that comes with this book. Does it work then?

I also notice you have a proeprty called id. Is that correct? The table column was initially called Id with a capital I.

Finally, you seem to be missing an InsertVisible="False" on the Id column in the FormView. You should hide that control as it's auto generated by the database (maybe you did make it a primary but not an identity?)

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
  #5 (permalink)  
Old July 5th, 2009, 06:01 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 object reference not set to instance of an object

Imar,
I did everything that you suggested including copying and pasting the code and I still get the same problem.

The only thing that I saw that in the database explorer the picture table as you correctly identified was "id" not "Id".

So I changed it to Id after I copied and pasted the source code.

When I first wrote to you I included a small piece of code which is as follows
PhotoAlbum myPhotoAlbum = (PhotoAlbum)e.Result;
Line 17: Response.Redirect(string.Format("ManagePhotoAlbum. aspx?PhotoAlbumId=(0)",
Line 18: myPhotoAlbum.id.ToString()));


The name of the file ManagephotoAlbum is spelt correctly so I am wondering
what the significance of the PhotoAlbumId=(0)",

code is? I cannot see a PhotoAlbumId variable in the database. What does =(0) mean?

I hope this is of help

Thanks

Thomas

Line 19: }
Reply With Quote
  #6 (permalink)  
Old July 5th, 2009, 06:07 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

The {0} is used in the string.Format call. Consider this:

Code:
 
string.Format("ManagePhotoAlbum.aspx?PhotoAlbumId=(0)", myPhotoAlbum.id.ToString())
This creates a string with a redirect URL based on the ID of the new photoalbum. So, if you insert a record in the database (you still haven't confirmed whether the record shows up in the database or not) its new ID is taken and appended to the redirect string. String.Format nicely puts the ID at the place of the {0} placeholder. So, when your new Album has an ID of 34, you're redirected to:

ManagePhotoAlbum.aspx?PhotoAlbumId=34

After you changed your database schema (column names etc) did you also regenerate the Linq to SQL diagram? And is your ID column a primary key AND an identity?

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 July 5th, 2009, 06:09 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

Correction

photoAlbumId is a variable in the Picture database.
Reply With Quote
  #8 (permalink)  
Old July 5th, 2009, 06:11 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
Correction

photoAlbumId is a variable in the Picture database
Heuh? I don't understand what you mean by that....

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 July 5th, 2009, 06:23 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,

Don't worry about that last comment.

I have made the Id column in the PhotoAlbum database an identity column by clicking properties and inserting the value "Id" next to the Identity Column cell in the properties window.

Still same problem.

BTW after cutting and pasting the code from the book I no longer have the Id text box in NewPhotoAlbum.aspx

Cheers Tome
Reply With Quote
  #10 (permalink)  
Old July 5th, 2009, 06:29 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 have just opened the website from the sample code that comes with the book and that works fine. Remember before that you asked be to copy and paste the code and it still didn't work well running it without copying and pasting seems to work.

I hope this helps localise the issue.

Regards

Thomas
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
Object reference not set to an instance of object iceman90289 ASP.NET 3.5 Basics 1 March 1st, 2009 05:13 AM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM
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:19 PM.


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