Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 24th, 2008, 05:01 PM
Registered User
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 13 4th Try It Out Delete Button Error

clicking the 'delete' button for any of the picture records does not delete the record and reload the page as I would expect but instead throws throws this error:

"Could not find a row that matches the given keys in the original values stored in ViewState. Ensure that the 'keys' dictionary contains unique key values that correspond to a row returned from the previous Select operation."

This would correspond to the number 17 item of that try it out which says: "Click the Delete button for an item and see how the item is removed from the list automatically."

I am not able to resolve this problem on my own because I have no idea what it means or how I would go about doing so. Any ideas?

 
Old August 25th, 2008, 12:55 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 full code for the page?

Maybe you forgot to set the DataKeyNames property? Or forgot to set a proper primary key in the database table?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old August 26th, 2008, 11:02 PM
Registered User
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sure. Here is the markup:

<%@ Page Language="C#" MasterPageFile="~/MasterPages/MasterPage.master" AutoEventWireup="true" CodeFile="ManagePhotoAlbum.aspx.cs" Inherits="ManagePhotoAlbum" Title="Manage Photo Album" %>

<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="LinqDataSource1" InsertItemPosition="LastItem">
        <LayoutTemplate>
            <ul class="itemContainer">
                <li ID="itemPlaceholder" runat="server" />
            </ul>
        </LayoutTemplate>
        <InsertItemTemplate>
            <li style="">
                Description:
                <asp:TextBox ID="DescriptionTextBox" runat="server"
                    Text='<%# Bind("Description") %>' />
                <br />
                Tooltip:
                <asp:TextBox ID="TooltipTextBox" runat="server" Text='<%# Bind("Tooltip") %>' />
                <br />
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <br />
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
                    Text="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                    Text="Clear" CausesValidation="false" />
            </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 />
                <asp:Image ID="ImageUrl" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
                <br />
                <asp:Button ID="DeleteButton" runat="server" CommandName="Delete"
                    Text="Delete" CausesValidation="false" />
            </li>
        </ItemTemplate>
    </asp:ListView>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server"
        ContextTypeName="PlanetWroxDataContext" EnableDelete="True" EnableInsert="True"
        TableName="Pictures" Where="PhotoAlbumId == @PhotoAlbumId"
            oninserting="LinqDataSource1_Inserting">
        <WhereParameters>
            <asp:QueryStringParameter DefaultValue="-1" Name="PhotoAlbumId"
                QueryStringField="PhotoAlbumId" Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>
</asp:Content>


and here is the code behind...

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ManagePhotoAlbum : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    {
        Picture myPicture = (Picture)e.NewObject;
        myPicture.PhotoAlbumId = Convert.ToInt32(Request.QueryString.Get("PhotoAlbu mId"));

        FileUpload FileUpload1 = (FileUpload)ListView1.InsertItem.FindControl("File Upload1");
        string virtualFolder = "~/GigPics/";
        string physicalFolder = Server.MapPath(virtualFolder);
        string fileName = Guid.NewGuid().ToString();
        string extension = System.IO.Path.GetExtension(FileUpload1.FileName);

        FileUpload1.SaveAs(System.IO.Path.Combine(physical Folder, fileName + extension));
        myPicture.ImageUrl = virtualFolder + fileName + extension;
    }
}


This is my code, but I also tried the included ManagePhotoAlbum.aspx and ManagePhotoAlbum.aspx.cs in the source/chapter13 folder you've provided and got the same error.

Gerard Torres
 
Old August 26th, 2008, 11:12 PM
Registered User
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oh and I do have the Id columns set as primary keys on both the PhotoAlbum and Picture tables. Also the isIdentity is set to 'yes' for those columns as well. The PhotoAlbumId column of the Picture table has not been given any special properties.

Gerard Torres
 
Old August 27th, 2008, 11:15 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 Gerard,

I tried my application with the code from your pages and it works as expected. So, the problem must be elsewhere.

Can you zip up the entire project, including the database and send it to me? If you need my e-mal address, check out the book's Intro or send me a message through my Profile page here on p2p.wrox.com

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old August 27th, 2008, 10:04 PM
Registered User
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sure I will get that sent to you. Thanks!

Gerard Torres
 
Old September 6th, 2008, 02:56 PM
Registered User
 
Join Date: Aug 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

SOLUTION:

Some others may experience this issue down the road.

Imar has discovered that the issue I was experiencing was being caused by a line of code in the Page_Load event of the file MasterPage.master code behind (either MasterPage.master.cs or MasterPage.master.vb). That line of code is

this.ID = "Master"; (in C#)

There are two simple solutions to this problem:
1. You can create an alternative event handler and place the same code into THAT event like this

    protected void Page_Init(object sender, EventArgs e)
    {
        this.ID = "Master";
    }
Of course you would then remove that line from the Page_Load event handler.
2. You can remove the line
    this.ID = "Master";
altogether as it is not required for the project to work correctly.

This line of code was suggested and is not a part of a 'try it out' in the book. The explanation for the use of the line can be found on page 276 in the book (Chapter 8) under a caveat.

I hope this helps anyone who comes across this in the future!

Gerard Torres
 
Old September 7th, 2008, 08:24 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Thanks for the update here, Gerard.

In the mean time, I wrote a little blog entry on my website about this topic. If you want to find out more, check out this post: http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=469

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
LINQ Error ? Chapter 13 workidoo BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 July 22nd, 2008 09:37 AM





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