And the problem is what exactly?
The problem is:
In the Try It Out, step 16, p510, I open NewPhotoAlbum.aspx in Internet Explorer, enter a name for the new photo album, and click Insert. ManagePhotoAlbum.aspx opens and I enter a description, tooltip, and "psuedo url" for the image url. The address bar includes the expected querystring:
http://localhost:49349/ManagePhotoAl...?PhotoAlbumId4. When I click on Insert, I get this error message:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum". The conflict occurred in database "C:\PKB\THETEACHINGENGINE\WEBDEVELOPMENTLEARNINGAS P.NET\BEGASPNET\SITE\APP_DATA\PLANETWROX.MDF", table "dbo.PhotoAlbum", column 'Id'.
The statement has been terminated.
Maybe you're not passing the correct key from NewPhotoAlbum.aspx to this page?
The QueryString in the address bar of the IE says: PhotoAlbumId4.
The source for NewPhotoAlbum.aspx.
vb is:
Code:
Imports PlanetWroxModel
Partial Class _NewPhotoAlbum
Inherits BasePage
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)
Response.Redirect(String.Format("ManagePhotoAlbum.aspx?PhotoAlbumId{0}",
myPhotoAlbum.Id.ToString()))
End Sub
End Class
Maybe the Query String field has a different name?
I assume the code above assigns the QueryString the name PhotoAlbumId.
The code below for ManagePhotoAlbum.aspx.
vb uses the same name:
Code:
Imports PlanetWroxModel
Partial Class _ManagePhotoAlbum
Inherits BasePage
Protected Sub EntityDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.EntityDataSourceChangingEventArgs) Handles EntityDataSource1.Inserting
Dim PhotoAlbumId As Integer =
Convert.ToInt32(Request.QueryString.Get("PhotoAlbumID"))
Dim myPicture As Picture = CType(e.Entity, Picture)
myPicture.PhotoAlbumId = PhotoAlbumId
End Sub
End Class
did you check the database and see if all key fields are named correctly and that you created the correct relationship between PhotoAlbum.Id and Picture.PhotoAlbumId?
Yes, when I open Database Diagrams/Reviews and Genres and see the diagrams for PhotoAlbum and Picture with all fields named correctly and the properties grid for FK_Picture_PhotoAlbum (when Tables and Columns Specifications is expanded) shows
Foreign Key Base Table = Picture
Foreign Key Columns = PhotoAlbumId
Primary/Unique Key Base Table = PhotoAlbum
Primary/Unique Key Columns = Id
If you make changes to the database, be sure to update the EF Data Model.
I did not make changes to the database but I tried to update the EF Data Model as follows (similar to step 4 on p 499):
> in Sol Expl, double click Site/App_Code/PlanetWrox.edmx
> see Photo Album and Picture are in diagram
> right click an empty space in diagram > Update Model From Database
> see Update wizard > Refresh tab > expand Tables
> see all four tables including PhotoAlbum and Picture
> Pluralize or Singularize checked > Include foreign key columns checked
> Finish
> confirmed that the Entity Set Names are Pictures and PhotoAlbums
> save PlanetWrox.edmx and web.config
One more thing: After finding that, in step 16, when I tried to insert a picture I would see the error message:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Picture_PhotoAlbum"
I looked at the downloaded source code for the end of Chapter 14. In that source for ManagePhotoAlbum.aspx I saw the <WhereParameters> of the <asp:EntityDataSource> the clause QueryStringField="PhotoAlbumId" which my version of ManagePhotoAlbum.aspx did not have. So, I added it. But IE still produced the same error message. Here is my source for ManagePhotoAlbum.aspx:
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="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id"
DataSourceID="EntityDataSource1" InsertItemPosition="LastItem">
<InsertItemTemplate>
<li style="">
<br />Description:
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
<br />ToolTip:
<asp:TextBox ID="ToolTipTextBox" runat="server" Text='<%# Bind("ToolTip") %>' />
<br />ImageUrl:
<asp:TextBox ID="ImageUrlTextBox" runat="server"
Text='<%# Bind("ImageUrl") %>' />
<br />
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</li>
</InsertItemTemplate>
<ItemTemplate>
<li style="">
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
ToolTip:
<asp:Label ID="ToolTipLabel" runat="server" Text='<%# Eval("ToolTip") %>' />
<br />
ImageUrl:
<asp:Label ID="ImageUrlLabel" runat="server" Text='<%# Eval("ImageUrl") %>' />
<br />
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
Text="Delete" />
</li>
</ItemTemplate>
<LayoutTemplate>
<ul class="ItemContainer">
<li runat="server" id="itemPlaceholder" />
</ul>
</LayoutTemplate>
</asp:ListView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=PlanetWroxEntities"
DefaultContainerName="PlanetWroxEntities" EnableDelete="True"
EnableFlattening="False" EnableInsert="True" EntitySetName="Pictures"
Where="it.PhotoAlbum.Id = @photoAlbumId">
<WhereParameters>
<asp:QueryStringParameter Name="PhotoAlbumId" QueryStringField="PhotoAlbumId"
Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cpClientScript" Runat="Server">
</asp:Content>
Is there any other information I can provide to you or any steps you advise me to take?
Cheers