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 23rd, 2011, 05:05 AM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Location: Ankara Turkey
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:
Quote:
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
Code:
<%@ 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>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ProductId" 
        DataSourceID="EntityDataSource1" InsertItemPosition="LastItem">
        
        
        <InsertItemTemplate>
            <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>
        </InsertItemTemplate>
        <ItemTemplate>
            <li style="">
                <br />
                ProductName:
                <asp:Label ID="ProductNameLabel" runat="server" 
                    Text='<%# Eval("ProductName") %>' />
                <br />
                CategoryId:
                <asp:Label ID="CategoryIdLabel" runat="server" 
                    Text='<%# Eval("CategoryId") %>' />
                <br />
                Brand:
                <asp:Label ID="BrandLabel" runat="server" Text='<%# Eval("Brand") %>' />
                <br />
                Model:
                <asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>' />
                <br />
                Size:
                <asp:Label ID="SizeLabel" runat="server" Text='<%# Eval("Size") %>' />
                <br />
                Price:
                <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
                <br />
                Tooltip:
                <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 />
                SystemInputDate:
                <asp:Label ID="SystemInputDateLabel" runat="server" 
                    Text='<%# Eval("SystemInputDate") %>' />
                <br />
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" 
                    Text="Delete" />
            </li>
        </ItemTemplate>
        <LayoutTemplate>
            <ul class="ItemContainer">
                <li runat="server" id="itemPlaceholder" />
            </ul>
            <div style="">
                <asp:DataPager ID="DataPager1" runat="server">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                            ShowNextPageButton="False" ShowPreviousPageButton="False" />
                        <asp:NumericPagerField />
                        <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                            ShowNextPageButton="False" ShowPreviousPageButton="False" />
                    </Fields>
                </asp:DataPager>
            </div>
        </LayoutTemplate>
        
    </asp:ListView>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=ProductsEntities3" 
        DefaultContainerName="ProductsEntities3" 
        EnableFlattening="False" EntitySetName="Products" 
        Where="it.PhotoAlbum.Id=@PhotoAlbumId" EnableDelete="True" 
        EnableInsert="True">
        <WhereParameters>
            <asp:QueryStringParameter Name="PhotoAlbumId" QueryStringField="PhotoAlbumId" 
                Type="Int32" />
        </WhereParameters>
    </asp:EntityDataSource>
</asp:Content>
the code in vb file is as follows

Code:
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:
Quote:
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

Quote:
(getdate())
Please Help

Thank you
Oruc
Reply With Quote
  #2 (permalink)  
Old August 23rd, 2011, 12:32 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: 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,

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

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

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
The Following User Says Thank You to Imar For This Useful Post:
oruckenan (August 23rd, 2011)
  #3 (permalink)  
Old August 23rd, 2011, 02:18 PM
Authorized User
Points: 52, Level: 1
Points: 52, Level: 1 Points: 52, Level: 1 Points: 52, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Location: Ankara Turkey
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
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
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



All times are GMT -4. The time now is 03:06 PM.


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