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 18th, 2012, 08:19 PM
Registered User
 
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 14 ListView

In the Try It Out, step 16, p510, I open NewPhotoAlbum.aspx in Internet Explorer, enter a name for the 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...?PhotoAlbumId3. (Third attempt.) 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.

I have recreated the aspx files and the tables carefully following each step in the book but I keep getting the same error message.

source for NewPhotoAlbum.aspx:
Code:
<%@ 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" runat="server" AutoGenerateRows="False" 
    DataKeyNames="Id" DataSourceID="EntityDataSource1" DefaultMode="Insert" 
    Height="50px" Width="125px">
    <Fields>
      <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" 
        InsertVisible="false" SortExpression="Id" />
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
      <asp:CommandField ShowInsertButton="True" />
    </Fields>
  </asp:DetailsView>
  <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=PlanetWroxEntities" 
    DefaultContainerName="PlanetWroxEntities" EnableFlattening="False" 
    EnableInsert="True" EntitySetName="PhotoAlbums">
  </asp:EntityDataSource>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cpClientScript" Runat="Server">
</asp:Content>
source for NewPhotoAlbum.aspx.vb:
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
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>
source for ManagePhotoAlbum.aspx.vb:
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
the connectionStrings in web.config:
Code:
	<connectionStrings>
		<add name="PlanetWroxConnectionString1" connectionString="Data Source=(local)\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PlanetWrox.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
	  <add name="PlanetWroxEntities" connectionString="metadata=res://*/App_Code.PlanetWrox.csdl|res://*/App_Code.PlanetWrox.ssdl|res://*/App_Code.PlanetWrox.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(local)\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PlanetWrox.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
I do not know how to show you code for the foreign key constraint FK_Picture_PhotoAlbum, but I can successfully add a Picture to an existing PhotoAlbum when the Picture's PhotoAlbumID = PhotoAlbum.Id using an SQL statement such as::
Code:
INSERT INTO Picture
                   (Description, ToolTip, ImageUrl, PhotoAlbumId)
VALUES        ('pic 3-1', 'tt', 'psuedo url', '3')
Please help.

Last edited by MarshallN; August 20th, 2012 at 11:10 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
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 25 Page 850 ListView ListView Frodo BOOK: Beginning Visual C# 2010 0 December 23rd, 2011 12:55 PM
Ch 14 Customizing Templates of the ListView Control leeWozyWarren BOOK: Beginning ASP.NET 4 : in C# and VB 3 December 4th, 2011 06:16 AM
Chapter 14, Listview control problem SamuelMSr BOOK: Beginning ASP.NET 4 : in C# and VB 3 September 28th, 2011 02:47 PM





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