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 March 6th, 2015, 02:45 AM
Registered User
 
Join Date: Mar 2015
Posts: 9
Thanks: 4
Thanked 1 Time in 1 Post
Default chapter 14,Try it out in page 505 and 513

Hi,
First of all this is an excellent book and I am thoroughly enjoying it. I am following Beginning ASP.NET 4.5.1: in C# and VB and using Visual studio 2013. I have doubt in Try it out in page 505 and 513 of chapter 14.

As per the instruction, I have drag and dropped DetailsView control in the cpMainContent placeholder and included all the code. But intellisense is not showing InsertMethod. I tried pressing Ctrl+space but no use. So I manually entered InsertMethod in the Markup and corresponding code in the code behind file. But I am getting below message and error.

Validation (ASP.Net): Attribute 'InsertMethod' is not a valid attribute of element 'DetailsView'
'TryUpdateModel' is not declared. It may be inaccessible due to its protection level.

I ignored above error and tried next Try it out. But similar error occurred in this example as well.
When I request NewPhotoAlbum.aspx, I am able to view the page but when I entered value and press insert button, I am getting below error.

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.

I am trying to figure out this since 2 days. Can you kindly tell me what might be causing this and how to fix it? Thanks in advance!
 
Old March 7th, 2015, 08:49 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Can you post the code for the page?

Also, make sure your site is error free (by checking the Error List).

Cheers,

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 March 7th, 2015, 12:37 PM
Registered User
 
Join Date: Mar 2015
Posts: 9
Thanks: 4
Thanked 1 Time in 1 Post
Default

Thank you..
Below is the code for the NewPhotoAlbum.aspx

<%@ Page Title="Create New Photo Album" Language="VB" MasterPageFile="~/Masterpages/Frontend.master" AutoEventWireup="false" CodeFile="NewPhotoAlbum.aspx.vb" Inherits="_NewPhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:DetailsView ID="DetailsView1" Height="50px" Width="125px" AutoGenerateRows="False" DefaultMode="Insert" runat="server" InsertMethod="DetailsView1_InsertItem">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:CommandField ShowInsertButton="True" ShowCancelButton="False" />
</Fields>
</asp:DetailsView>
</asp:Content>

Code behind :


Partial Class _NewPhotoAlbum
Inherits BasePage

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

End Sub
Public Sub DetailsView1_InsertItem()
Dim photoAlbum As New PhotoAlbum()
TryUpdateModel(photoAlbum)
If ModelState.Isvalid Then
Using myEntities As New PlanetWroxEntities()
myEntities.PhotoAlbums.Add(photoAlbum)
myEntities.SaveChanges()
End Using
Response.Redirect(String.Format("ManagePhotoAlbum? PhotoAlbumId={0}", photoAlbum.Id.ToString()))
End If
End Sub
End Class


Below is the code for ManagePhotoAlbum.aspx

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

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:ListView ID="ListView1" DataKeyNames="Id" InsertItemPosition="LastItem" runat="server" 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("ImageUrl")%>' /> <br />
<asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName="Insert" />
</li>
</InsertItemTemplate>
<ItemTemplate>
<li>
Description: <asp:Label ID="Description" runat="server" Text='<%# Eval("Description")%>' /><br />
ToolTip: <asp:Label ID="ToolTip" runat="server" Text='<%# Eval("ToolTip")%>' /><br />
ImageUrl: <asp:Label ID="ImageUrl" runat="server" Text='<%# Eval("ImageUrl")%>' /><br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="false" />
</li>
</ItemTemplate>
<LayoutTemplate>
<ul class="ItemContainer">
<li runat="server" id="itemPlaceholder"/>
</ul>
</LayoutTemplate>
</asp:ListView>
</asp:Content>

Code behind:
Partial Class _ManagePhotoAlbum
Inherits BasePage

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
End Sub
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)
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
 
Old March 7th, 2015, 04:59 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

This all looks fine to me. Are you sure there's nothing else in your site breaking things? (code errors elsewhere, invalid files in App_Code etc)?

Try removing this page from the site by moving it to another folder. Does the site build then?

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 March 8th, 2015, 01:47 AM
Registered User
 
Join Date: Mar 2015
Posts: 9
Thanks: 4
Thanked 1 Time in 1 Post
Default

Thank You.

I have rechecked the App_Code and compared it with the downloaded code for this chapter. I can see two extra files in App_code named ModelState.vb and QueryStringAttribute.vb. I am not sure how this file got into this folder as I am following the book line by line. Below are the codes.

Code in ModelState.vb

Imports Microsoft.VisualBasic

Public Class ModelState

Shared Function IsValid() As Boolean
Throw New NotImplementedException
End Function

End Class

Code in QueryStringAttribute.vb

Imports Microsoft.VisualBasic

Public NotInheritable Class QuerystringAttribute
Inherits Attribute

Private _p1 As String

Sub New(p1 As String)
' TODO: Complete member initialization
_p1 = p1
End Sub

End Class

Is this the problem?
 
Old March 8th, 2015, 09:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You shouldn't need those files. Did you try deleting them?

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 March 8th, 2015, 12:08 PM
Registered User
 
Join Date: Mar 2015
Posts: 9
Thanks: 4
Thanked 1 Time in 1 Post
Default

Hi,

I deleted those files and I am getting one more error as below apart from already existing ones.

Type 'Querystring' is not defined.

Can these errors be caused due to incorrect adding of tables in sql server management studio or database diagram? Can you kindly let me know how can I delete these tables and corresponding code from database diagram and tt file so that I can try adding it again?
 
Old March 8th, 2015, 04:35 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Where do you use the type Querystring? Maybe you added this as an experiment at some point? It's missing now since you deleted it from App_Code but I wonder how it got there in the first place.

Cheers,

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 March 9th, 2015, 05:59 PM
Registered User
 
Join Date: Mar 2015
Posts: 9
Thanks: 4
Thanked 1 Time in 1 Post
Default

Hi,
Thank you for the replies.
I am not sure what is wrong with code as I coded as per the instruction in the book. So I have decided to redo the Try it out again. So I removed table PhotoAlbum and Picture from the diagram by double clicking the PlanetWrox.edmx in App_code and then right click on each table and selected the option 'Remove from diagram'.
Again I tried to add these tables in the diagram by choosing Update Model from Database but in the wizard that appears these two table names are not there under the dbo.
I know I have completely messed up. Can you kindly let me know how to add these tables in the diagram again?
 
Old March 11th, 2015, 11:20 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Remove from diagram only removes them the diagram itself, but not from the model. Try opening the Model Browser (right-click an empty spot of the model designer and choose Model Browser). Do you see your tables under PlanetWroxModel | Entity Types? You can delete them from there and then refresh the database.

Cheers,

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!
The Following User Says Thank You to Imar For This Useful Post:
shameema (March 26th, 2015)





Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 14 try out page 497 marysunish BOOK: Beginning ASP.NET 4 : in C# and VB 2 May 11th, 2012 04:01 PM
chapter 14 Try It Out pg 505 step #4 arodrigu12 BOOK: Beginning ASP.NET 4 : in C# and VB 3 April 21st, 2011 02:28 PM
Including NameTable Optimization (page 513) DBoucherie BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 0 April 20th, 2011 09:19 AM
Try it out on page 505 fredfenk BOOK: Beginning ASP.NET 4 : in C# and VB 7 April 13th, 2011 03:16 PM
Chapter 1 page 14 kermit1965 BOOK: Professional ASP.NET MVC 2 6 October 12th, 2010 10:10 AM





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