View Single Post
  #3 (permalink)  
Old July 5th, 2009, 05:29 AM
tomche tomche is offline
Authorized User
Points: 221, Level: 4
Points: 221, Level: 4 Points: 221, Level: 4 Points: 221, Level: 4
Activity: 2%
Activity: 2% Activity: 2% Activity: 2%
 
Join Date: Apr 2009
Posts: 50
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