Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
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 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 August 23rd, 2011, 05:05 AM
Authorized User
Join Date: Jul 2011
Posts: 13
Thanks: 7
Thanked 0 Times in 0 Posts
Default Getting Error: Object reference not set to an instance of an object.

Hi Imar,

I have the following error:
Object reference not set to an instance of an object.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object
Line 9: myPicture.PhotoAlbumId = PhotoAlbumId
The code in the file ManagePhotoAlbum.aspx is as follows
<%@ Page Title="Manage Photo Album" Language="VB" Debug="true" MasterPageFile="~/MasterPages/ProdSupervisor.master" AutoEventWireup="false" CodeFile="ManagePhotoAlbum.aspx.vb" Inherits="SPV_Product_ManagePhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ProductId" 
        DataSourceID="EntityDataSource1" InsertItemPosition="LastItem">
            <li style="">
                <br />ProductName:
                <asp:TextBox ID="ProductNameTextBox" runat="server" 
                    Text='<%# Bind("ProductName") %>' />
                <br />CategoryId:
                <asp:TextBox ID="CategoryIdTextBox" runat="server" 
                    Text='<%# Bind("CategoryId") %>' />
                <br />Brand:
                <asp:TextBox ID="BrandTextBox" runat="server" Text='<%# Bind("Brand") %>' />
                <br />Model:
                <asp:TextBox ID="ModelTextBox" runat="server" Text='<%# Bind("Model") %>' />
                <br />Size:
                <asp:TextBox ID="SizeTextBox" runat="server" Text='<%# Bind("Size") %>' />
                <br />Price:
                <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
                <br />Tooltip:
                <asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
                <br />ImageUrl:
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <br />SystemInputDate:
                <br />
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                    Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Clear" />
            <li style="">
                <br />
                <asp:Label ID="ProductNameLabel" runat="server" 
                    Text='<%# Eval("ProductName") %>' />
                <br />
                <asp:Label ID="CategoryIdLabel" runat="server" 
                    Text='<%# Eval("CategoryId") %>' />
                <br />
                <asp:Label ID="BrandLabel" runat="server" Text='<%# Eval("Brand") %>' />
                <br />
                <asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>' />
                <br />
                <asp:Label ID="SizeLabel" runat="server" Text='<%# Eval("Size") %>' />
                <br />
                <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
                <br />
                <asp:Label ID="TooltipLabel" runat="server" Text='<%# Eval("Tooltip") %>' />
                <br />
<%--                <asp:Label ID="ImageUrlLabel" runat="server" TextBox='<%# Eval("ImageUrl") %>' />
                <asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />

                <br />
                <asp:Label ID="SystemInputDateLabel" runat="server" 
                    Text='<%# Eval("SystemInputDate") %>' />
                <br />
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
            <ul class="ItemContainer">
                <li runat="server" id="itemPlaceholder" />
            <div style="">
                <asp:DataPager ID="DataPager1" runat="server">
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                            ShowNextPageButton="False" ShowPreviousPageButton="False" />
                        <asp:NumericPagerField />
                        <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                            ShowNextPageButton="False" ShowPreviousPageButton="False" />
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        EnableFlattening="False" EntitySetName="Products" 
        Where="it.PhotoAlbum.Id=@PhotoAlbumId" EnableDelete="True" 
            <asp:QueryStringParameter Name="PhotoAlbumId" QueryStringField="PhotoAlbumId" 
                Type="Int32" />
the code in vb file is as follows

Imports ProductsModel

Partial Class SPV_Product_ManagePhotoAlbum
    Inherits System.Web.UI.Page

    Protected Sub EntityDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangingEventArgs) Handles EntityDataSource1.Inserting
        Dim PhotoAlbumId As Integer = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"))
        Dim myPicture As Product = CType(e.Entity, Product)
        myPicture.PhotoAlbumId = PhotoAlbumId

        'For saving file to disk page 516
        'updates the imageurl property of the picture instance 
        '                       BEGIN
        Dim FileUpload1 As FileUpload = CType(ListView1.InsertItem.FindControl("FileUpload1"), FileUpload)
        Dim virtualFolder As String = "~/images/Products/"
        Dim physicalFolder As String = Server.MapPath(virtualFolder)
        Dim fileName As String = Guid.NewGuid().ToString()
        Dim extension As String = System.IO.Path.GetExtension(FileUpload1.FileName)
        FileUpload1.SaveAs(System.IO.Path.Combine(physicalFolder, fileName & extension))
        myPicture.ImageUrl = virtualFolder & fileName & extension
        '                       page 516
        '                       END
    End Sub

    Protected Sub ListView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles ListView1.ItemInserting
        'Dim FileUpload1 As FileUpload = CType(ListView1.InsertItem.FindControl("FileUpload1"), FileUpload)
        'If Not FileUpload1.HasFile OrElse
        'Not FileUpload1.FileName.ToLower().EndsWith(".jpg") Then
        '    Dim cusValImage As CustomValidator =
        '    CType(ListView1.InsertItem.FindControl("cusValImage"), CustomValidator)
        '    cusValImage.IsValid = False
        '    e.Cancel = True
        'End If
    End Sub
End Class
Table name Product field list is as follows:
table name : Product
fields name: ProductId, ProductName, CategoryId, Brand, Model, Size, Price, Tooltip, ImageUrl, PhotoAlbumId, SystemInputDate
SystemInputDate field in the table has a Default Value or Binding property set to as follows

Please Help

Thank you
Old August 23rd, 2011, 12:32 PM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi there,

Are you successfully passing a PhotoAlbumId in the query string to this page? In other words, does this line of code:

Dim PhotoAlbumId As Integer = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumId"))
result in a valid and existing Album ID?

Imar Spaanjaars
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!
The Following User Says Thank You to Imar For This Useful Post:
oruckenan (August 23rd, 2011)
Old August 23rd, 2011, 02:18 PM
Authorized User
Join Date: Jul 2011
Posts: 13
Thanks: 7
Thanked 0 Times in 0 Posts
Default resolved:my mistake of input data - sorry

Hi Imar,

I found the mistake; I am sorry for this. When I was trying to get the results of the page I entered the correct things and everyhting went well
The price text box is in the table an integer.
In the page I was inputting for example "40usd" which is not an int. And so I was getting an error, I was skipping the validators in my page and I am trapped with my own mistakes.

Sorry for this and really thank you very much

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error - Object reference not set to an instance of an object Phil Critchley ASP.NET 4 General Discussion 2 April 30th, 2011 05:18 AM
Object reference not set to an instance of an object. dotnet_user C# 2008 aka C# 3.0 2 April 6th, 2011 06:11 AM
Chap 13 pg448: Object reference not set to an instance of an object tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 15 August 8th, 2009 06:14 AM
Error - Object reference not set to an instance ? venkikrao VB.NET 2 July 5th, 2007 01:00 AM
Error: Object Reference not set to an instance. Nosfe_X VB Databases Basics 2 June 23rd, 2006 04:10 PM

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