Wrox Programmer Forums
|
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 April 26th, 2017, 08:03 PM
Authorized User
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default Ch. 14 - Page 516: ModelState

Per Chapter 14, page 516 instructions, I have inserted the following code below into the ManagePhotoAlbum code-behind file. I am getting red, squiggly lines under the bold parts of this code.:

Code:
  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)
    If ModelState.IsValid Then
      Using myEntities As New PlanetWroxEntities
        picture.PhotoAlbumId = photoAlbumId
        myEntities.Pictures.Add(picture)
        myEntities.SaveChanges()
      End Using
    End If

  End Sub
These are the following errors:

QueryString: Type 'QueryString' is not defined.
TryUpdateModel: 'TryUpdateModel' is not declared. It may be inaccessible due to its protected level.
ModelState: 'ModelState' is not declared. It may be inaccessible due to its protected level.

When I attempt to run the app, I get the following error:

Quote:
A null value for parameter 'photoAlbumId' of non-nullable type 'System.Int32' for method 'System.Linq.IQueryable ListView1_GetData(Int32)' in '_ManagePhotoAlbum'. An optional parameter must be a reference type or a nullable type.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: A null value for parameter 'photoAlbumId' of non-nullable type 'System.Int32' for method 'System.Linq.IQueryable ListView1_GetData(Int32)' in '_ManagePhotoAlbum'. An optional parameter must be a reference type or a nullable type.
 
Old April 29th, 2017, 04:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Did you see the following in Step 6 on page 515:

Quote:
Note that you need to add a using/Imports statement for the namespace System.Web.ModelBinding to bring the QueryString attribute into scope. You can either type in this namespace manually at the top of the code file, or you can press Ctrl+. (Ctrl+dot) on the word QueryString in the method definition and pick the namespace from the list that appears.
Hope this helps,

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!
 
Old April 29th, 2017, 08:59 AM
Authorized User
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default Model Binding

Dear Imar:

Thanks for the quote from your book, but I already had that in my imports statement.
 
Old April 30th, 2017, 10:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In that case, can you post the full code for the page?
__________________
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!
 
Old April 30th, 2017, 12:06 PM
Authorized User
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default Model Binding

My code is below. One note that I'm seeing now, didn't catch before - the System.Web.ModelBinding line is getting a message when you hover over it.
The message is - Namespace does not contain any public member or cannot be found. Make sure the imported namespace doesn't contain any aliases.

I'm going to google this, but isn't System.Web.ModelBinding a typical namespace to include?

Here is the code on the code-behind file:

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)
    If ModelState.IsValid Then
      Using myEntities As New PlanetWroxEntities
        picture.PhotoAlbumId = photoAlbumId
        myEntities.Pictures.Add(picture)
        myEntities.SaveChanges()
      End Using
    End If

  End Sub

  Public Sub ListView1_DeleteItem(ByVal id As Integer)

  End Sub

End Class
Here is the code in the source file:

Code:
<%@ Page Title="Manage Photo Album" Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="ManagePhotoAlbum.aspx.vb" Inherits="_ManagePhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cpMainContent" Runat="Server">
  <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" InsertItemPosition="LastItem" 
                  SelectMethod="ListView1_GetData" InsertMethod="ListView1_InsertItem" 
                  DeleteMethod="ListView1_DeleteItem">
    <InsertItemTemplate>
      <li>
        Description: <asp:TextBox ID="Description" runat="server" TextMode="MultiLine" 
                        Text='<%# Bind("Description") %>' /><br />
        ToolTip:  <asp:TextBox ID="ToolTip" runat="server"
                        Text='<%# Bind("ToolTip") %>' /><br />
        ImageUrl: <asp:TextBox ID="ImageUrl" runat="server"
                        Text='<%# Bind("ImageUrl") %>' /><br />
        <asp:Button ID ="InsertButton" runat="server" Text="Insert" CommandName="Insert" />
      </li>
    </InsertItemTemplate>
    <ItemTemplate>
      <li>
        Description: <asp:TextBox ID="Description" runat="server" TextMode="MultiLine" 
                        Text='<%# Eval("Description") %>' /><br />
        ToolTip:  <asp:TextBox ID="ToolTip" runat="server"
                        Text='<%# Eval("ToolTip") %>' /><br />
        ImageUrl: <asp:TextBox ID="ImageUrl" runat="server"
                        Text='<%# Eval("ImageUrl") %>' /><br />
        <asp:Button ID ="DeleteButton" runat="server" Text="Delete" CommandName="Delete" />
      </li>
    </ItemTemplate>
    <LayoutTemplate>
      <ul class="ItemContainer">
        <li runat="server" id="ItemPlaceHolder" />
      </ul>
    </LayoutTemplate>
  </asp:ListView>
</asp:Content>
 
Old April 30th, 2017, 12:18 PM
Authorized User
 
Join Date: Mar 2017
Posts: 55
Thanks: 12
Thanked 0 Times in 0 Posts
Default target framework

Okay, my target framework was 4.0 and not 4.5 - not sure how that happened, but I should be good to go now.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch 14: Page 511 - Validating the DetailView papadan BOOK: Beginning ASP.NET 4.5.1 : in C# and VB 1 April 18th, 2017 07:42 AM
Ch.14 first try it out lrk89 BOOK: Beginning ASP.NET 4 : in C# and VB 7 February 10th, 2014 05:12 PM
Problem with ModelView and ModelState.IsValid on [HttpPost]Create bigasscactus BOOK: Professional ASP.NET MVC 2 1 July 14th, 2011 01:09 PM
Ch 14 Page 485 Try-It-Out problem ken evans BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 January 17th, 2010 07:23 AM
Error in code Chapter 13 Page 516 neubauet BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 9 June 11th, 2006 11:06 AM





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