Wrox Programmer Forums
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 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 August 26th, 2012, 11:40 AM
Registered User
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 14 ListView - part 2

Dear fellow programmers,
47 readers have read my 8/18/2012 posting, "Chapter 14 ListView", but I have not received any suggestions.

In the Try It Out, step 16, p510, when I get to the point on ManagePhotoAlbum.aspx and click on the Insert button, I get an error message instead of seeing the ListView showing my psuedo picture.

So, on 8/22, I decided to create a new website, a database with only the two tables PhotoAlbum and Pictures, and go through every step from there. I still get the same error message:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum".

PLEASE help.

Here are the steps I took:
Create New WebSite2012-08-22:
> in VWD, File > New File > see New Web Site dialog
> VB, ASP.NET Empty Web Site, Web Location = C:\PKB\TheTeachingEngine\WebDevelopmentLearningASP .Net\WebSite2012-08-22
> OK

Create DB and Tables:
> in VWD, WebSite2012-08-22, Sol Expl > right click WebSite2012-08-22
> Add ASP.NET Folder > App_Data > in Sol Expl, right click App_Data folder
> Add New item
> VB, SQL Server Database, Db2012-08-22.mdf, click Add
> in DbExpl, right click Tables > Add New Table
> in properties, Name = PhotoAlbum (changed from Table1)
> in main window, Column Name = Id, Data Type = int, uncheck Allow Nulls
> click left arrow to select Id column
> click gold key to set as Primary Key
> in Column Properties, expand Identity Specification
> set IsIdentity = Yes
> automatically, Identity Seed = 1 and Identity Increment = 1
> add second column: Column Name = Name, Data Type = nvarchar(100), uncheck Allow Nulls
> save and repeat for Picture table with these columns:
Id, int, uncheck Allow Nulls, primary key, IsIdentity = Yes;
Description, nvarchar(300), uncheck Allow Nulls;
ToolTip, nvarchar(50), uncheck Allow Nulls;
ImageUrl, nvarchar(200), uncheck Allow Nulls;
PhotoAlbumId, int, uncheck Allow Nulls (to be foreign key).
> save and close.

Foreign Key:
Now, to make PhotoAlbumID a foreign key:
> in Db Expl, expand Db2012-08-22.mdf
> right click Database Diagrams > Add New Diagram
> see: "Server does not have objects for diagramming. Create them?"
> Yes
> see Add Table dialog
> with PhotoAlbum selected, click Add
> Picture table is selected > Add > Close
> see: Database Diagram has the PhotoAlbum and Picture tables
> drag PhotoAlbum.Id to Picture.PhotoAlbumId
> see: Tables and Columns dialog
with Relationship name = FK_Picture_PhotoAlbum,
Primary = PhotoAlbum.Id, and Foreign Key = Picture.PhotoAlbumId:
> OK
> see: Foreign Key Relationship dialog > OK
> see: Database Diagram has Foreign Key Relationship drawn
with gold key pointing to PhotoAlbum and infinity pointing Picture
> save
> see: Choose Name dialog with Diagram Name = PhotoAlbum_Picture:
> OK
> see: Save dialog with tables = PhotoAlbum and Picture
and Warn About Tables Affected checked
> Yes
> see: Database Diagram again
> Save and Close.

Entity Framework Model:
> right click WebSite2012-08-22 > Add ASP.NET folder > App_Code
> right click App_Code folder > Add New Item
> VB, ADO.NET Entity Data Model, Name = WebSite2012-08-22.edmx >Add
> see Entity Data Model Wizard > click Generate From Database > Next
> see: Choose Your Data Connection dialog
with Connection = Db2012-08-22.mdf, Save in Web.Config checked,
As = WebSite20120822Entities
Note: apparently, dashes are not allowed in Entity connection string name.
> Next > after hesitation, see: Choose Your Database Objects dialog
> check Tables, check Pluralize Singularize Names,
check Include Foreign Keys, Model Namespace = WebSite20120822Model
Note: apparently, dashes are not allowed in Model Namespace.
> Finish
> see: Entity Framework diagram App_Code/WebSite2012-08-22.edmx
with PhotoAlbum and Picture tables
and the line connecting them showing the tooltip
"Source: PhotoAlbum(1), Target: Picture(*)".
> Note: Entity Set Names automatcially set to Pictures and PhotoAlbums.

<?xml version="1.0" encoding="utf-8"?>
  For more information on how to configure your ASP.NET application, please visit
    <compilation debug="false" strict="false" explicit="true" targetFramework="4.0">
        <add assembly="System.Security, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Data.Entity, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add assembly="System.Data.Entity.Design, Version=, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
        <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
    <add name="WebSite20120822Entities" connectionString="metadata=res://*/App_Code.WebSite2012-08-22.csdl|res://*/App_Code.WebSite2012-08-22.ssdl|res://*/App_Code.WebSite2012-08-22.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Db2012-08-22.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
> right click WebSite2012-08-22 > Add New Item
> VB, WebForm, NewPhotoAlbum.aspx > Add
> in source, title = "Create New Photo Album"
> in design, drag Toolbox/Data/DetailsView to <div>
> Smart Tasks panel > Choose Data Source = <New Data Source>
> see Data Source Configuration wizard
> Entity, Specify an ID = EntityDataSource1 > OK
> see Configure ObjectContext dialog
> Named Connection dropdown list = WebSite20120822Entities
> Next
> see Configure Data Selection dialog > EntitySetName = PhotoAlbums
> check Enable Automatic Inserts (to add behavior)
> Finish
> see NewPhotoAlbum.aspx in design view
> in Smart Tasks panel, Refresh Schema
Note: the columns in DetailsView are now named by field.
> in Smart Tasks, check Enable Inserting
> in DetailsView1/properties grid, DefaultMode=Insert
> in source, add Id column property InsertVisible="False"
> in design, select EntityDataSource1 to open its property grid
> Events tab (gold lightning bolt) > double click Inserted event
> see EntityDataSource1_Inserted event
> at top of code file, Imports WebSite20120822Model
> in EntityDataSource1_Inserted, add this code to direct user to another page after inserting a PhotoAlbum:
Imports WebSite20120822Model

Partial Class NewPhotoAlbum
    Inherits System.Web.UI.Page

  Protected Sub EntityDataSource1_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangedEventArgs) Handles EntityDataSource1.Inserted
    Dim myPhotoAlbum As PhotoAlbum = CType(e.Entity, PhotoAlbum)
  End Sub
End Class
> right click WebSite2012-08-22 > Add New Item
> VB, MyBasePage, ManagePhotoAlbum.aspx > Add
> set Title="Manage Photo Album"
> in design, drag Toolbox/Data/ListView to <div>
> in Smart Tasks, Choose Data Source = <New data source>
> see Data Source Configuration wizard > Entity, EntityDataSource1
> OK
> see Configure ObjectContext dialog
> Named Connection = WebSite20120822Entities
> Next
> see Configure Data Selection dialog
of Entity DataSource1's Configure Data Source wizard
> EntitySetName = Pictures
> check Automatic Inserts, check Automatic Deletes > Finish
> back in design, select (click) EntityDataSource1
> in properties grid, click Where > click elipses to open its Expression Editor
Recall: Picture.PhotoAlbumId is the foreign key pointing to PhotoAlbum.Id;
ManagePhotoAlbum.aspx receives the PhotoAlbum.Id through the querystring when it is called so it can filter the photos for the correct PhotoAlbum.
> click Add Parameter > enter PhotoAlbumID
> Parameter Source = QueryString > QueryStringField = PhotoAlbumId
> Where Expression (at top) = it.PhotoAlbum.Id = @photoAlbumId
> Show Advanced Properties > Type = Int32
> OK > see ManagePhotoAlbum.aspx again
> in design, select (click) ListView1 > Smart Tasks
> Refresh Schema – wait a moment – reopen the Smart Tasks panel
> see Configure ListView now an available option on Smart Tasks:
> click Configure ListView > see Configure ListView dialog
> Layout = BulletedList, enable Inserting and Deleting
> OK
> in source view, delete these templates:
> in <LayoutTemplate>,
> in <ul> element, delete ID, runat, and style attributes
> remove empty Div below the </ul>
> add class="ItemContainer"
> in <ItemTemplate>,
> delete lines for Id, PhotoAlbumId, PhotoAlbum
> Repeat for <InsertItemTemplate>

> in design, select EntityDataSource1
> in properties grid, Events tab
> double click Inserting > see ManagePhotoAlbum.aspx.vb code
> insert Imports WebSite20120822Model and add this code:
Imports WebSite20120822Model

Partial Class 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 =
    Dim myPicture As Picture = CType(e.Entity, Picture)
    myPicture.PhotoAlbumId = PhotoAlbumId
  End Sub
End Class
> save all

> in Sol Expl, right click WebSite2012-08-22 > Add New Item
> VB, Text File, name = Styles.css > Add
> add these rule sets:
  width: 600px;
  list-style-type: none;
.ItemContainer li
  height: 300px;
  width: 200px;
  float: left;
.ItemContainer li img
  width: 180px;
  margin: 10px, 20px, 10px, 0;
> add this link to <head> section of each aspx:
    <link href="../Styles.css" rel="stylesheet" type="text/css" />
NewPhotoAlbum.aspx opens in IE correctly.
ManagePhotoAlbum.aspx opens in IE correctly.
However, Inserting a picture record results in same error message again!

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 ListView MarshallN BOOK: Beginning ASP.NET 4 : in C# and VB 0 August 18th, 2012 08:19 PM
Chapter 14 Paging Data with the Listview and DataPager Controls rmanapul BOOK: Beginning ASP.NET 4 : in C# and VB 4 May 22nd, 2012 04:10 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
Chapter 14, Listview control problem SamuelMSr BOOK: Beginning ASP.NET 4 : in C# and VB 3 September 28th, 2011 02:47 PM
Chapter 14: Pages 491-492: Part of row doesn't show the black background gfmann BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 14 March 3rd, 2010 10:42 AM

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