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 September 1st, 2012, 02:57 PM
Registered User
 
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 14 ListView - part 3

I seem to have narrowed down the problem to a single line of code.
In ManagePhotoAlbum.aspx.vb, if I replace
Code:
    Dim PhotoAlbumId As Integer = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumID"))
with
Code:
    Dim PhotoAlbumId As Integer = 1
then ManagePhotoAlbum.aspx allows inserting Pictures.
Of course, this results in pictures being added only to the PhotoAlbum whose Id = 1.
I can repeat the process and add new photo albums with higher Id values which are passed by QueryString to ManagePhotoAlbum.aspx but, naturally, the higer Id value is ignored.
I thought, perhaps, the problem is in the conversion. However, the following produced the same old "Foreign Key contraint" error message as described in the two previous postings:
"Chapter 14 ListView" on August 18, 2012
and "Chapter 14 ListView - part 2" on August 26.

Code:
    Dim PhotoAlbumId As Integer = Request.QueryString.Get("PhotoAlbumID")
    Dim myPicture As Picture = CType(e.Entity, Picture)
    myPicture.PhotoAlbumId = PhotoAlbumId
and
Code:
    Dim PhotoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbumID"))
    Dim myPicture As Picture = CType(e.Entity, Picture)
    myPicture.PhotoAlbumId = PhotoAlbumId
I am stuck in the Try It Out, step 16, p510! Please please help.
 
Old September 1st, 2012, 03:14 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
I seem to have narrowed down the problem to a single line of code.
And the problem is what exactly?

Maybe you're not passing the correct key from NewPhotoAlbum.aspx to this page? Maybe the Query String field has a different name?

Also, 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? If you make changes to the database, be sure to update the EF Data Model.

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 September 2nd, 2012, 10:25 AM
Registered User
 
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default Chapter 14 ListView - part 3 (continued)

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
 
Old September 22nd, 2012, 01:20 PM
Registered User
 
Join Date: Mar 2011
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
Default And the answer is ....

In NewPhotoAlbum.aspx.vb
Code:
  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
the equal sign was missing in the Response.Redirect statement.

This omission results in ManagePhotoAlbum.aspx.vb being unable to access the querystring value and , so, places a zero in the Picture.PhotoAlbumId field which, in turn, results in the foreign key constraint error message.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 14 ListView - part 2 MarshallN BOOK: Beginning ASP.NET 4 : in C# and VB 0 August 26th, 2012 11:40 AM
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





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