Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
|
BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5.1: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-84677-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5.1 : 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 February 5th, 2018, 07:05 AM
Registered User
 
Join Date: Feb 2018
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 14: CUSTOMIZING TEMPLATES OF THE LISTVIEW CONTROL

I have followed the book but when I try to add a photo by clicking the insert button. I get an error on the underlined line of code. How can I fix this?

the error I get:
An exception of type 'System.NullReferenceException' occurred in App_Web_fc2yp5zf.dll but was not handled in user code

System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=App_Web_fc2yp5zf
StackTrace:
at _ManagePhotoAlbum.ListView1_InsertItem(Int32 photoAlbumId) in C:\BegASPNET\PlanetWrox\ManagePhotoAlbum.aspx.vb:l ine 19
InnerException:


Code:
Imports System.Web.ModelBinding

Partial Class _ManagePhotoAlbum
    Inherits BasePage

    Public Function ListView1_GetData(<QueryString("PhotoAlbumId")> photoAlbumId As Integer) As IQueryable
        Dim myEntities As New PlanetWroxEntities()
        Return From p In myEntities.Pictures
               Where p.PhotoAlbumId = photoAlbumId
               Select p
    End Function

    Public Sub ListView1_InsertItem(<QueryString("PhotoAlbumId")> photoAlbumId As Integer)
        Dim picture As New Picture()
        TryUpdateModel(picture)

        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
            ModelState.AddModelError("Invalid file type", cusValImage.ErrorMessage)
        End If

        If ModelState.IsValid AndAlso Page.IsValid Then
            Using myEntities As New PlanetWroxEntities()
                picture.PhotoAlbumId = photoAlbumId

                Dim virtualFolder As String = "~/GigPics/"
                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))
                picture.ImageUrl = virtualFolder + fileName + extension

                myEntities.Pictures.Add(picture)
                myEntities.SaveChanges()
            End Using
        End If
    End Sub


    Public Sub ListView1_DeleteItem(ByVal id As Integer)
        Using myEntities As New PlanetWroxEntities()
            Dim picture = (From p In myEntities.Pictures
                           Where p.Id = id
                           Select p).Single()
            myEntities.Pictures.Remove(picture)
            myEntities.SaveChanges()
        End Using
    End Sub
End Class
Found what was wrong. I forgot to change the ID in step 3.

Last edited by acydburn01; February 5th, 2018 at 07:16 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
(P. 515) CustomValidator in "Customizing Templates of the Listview Control" Tulsi BOOK: Beginning ASP.NET 4 : in C# and VB 1 November 2nd, 2012 06:08 AM
Chapter 14 Customizing Templates of the ListView Control rmanapul BOOK: Beginning ASP.NET 4 : in C# and VB 7 May 21st, 2012 04:42 AM
CH14 Customizing Templates of the ListView Control (image issue) agiusthomas BOOK: Beginning ASP.NET 4 : in C# and VB 3 April 16th, 2012 03:15 PM
Ch 14 Customizing Templates of the ListView Control leeWozyWarren BOOK: Beginning ASP.NET 4 : in C# and VB 3 December 4th, 2011 06:16 AM
Chapter 14, Listview control problem SamuelMSr BOOK: Beginning ASP.NET 4 : in C# and VB 3 September 28th, 2011 02:47 PM





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