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.5.1 > BOOK: Beginning ASP.NET 4.5.1 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 Display Modes
  #1 (permalink)  
Old February 5th, 2018, 06:05 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 06:16 AM.
Reply With Quote
Reply


Thread Tools
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
(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 05:16 AM
Chapter 14, Listview control problem SamuelMSr BOOK: Beginning ASP.NET 4 : in C# and VB 3 September 28th, 2011 02:47 PM



All times are GMT -4. The time now is 05:21 PM.


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