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 December 12th, 2009, 09:04 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default modifying the AddEditReview to include a PhotoAlbum

Hi Imar,

I was trying to modify the Reviews.aspx, with the taught im mind that it would be pretty neat to have a dropdownlist for people to add a photoAlbum to a review. (ok make a photoAlbum first to suit the review is second)
So I included a PhotoAlbumId column in the Review table, set up the FK relationship in the diagram, next in the datacontext I threw out the Review and PhotoAlbum, to make sure that the changes are persisted also there.

Next step was to make a template of the new PhotoAlbumId column in the DetailsView, and put ddl's both in the update and in the insert templates.

All seems to work, except that it does not make an insert in the database, it does not throw an error, the insert simply does not take place.... update however works fine.

My code for the AddEditReview.aspx page looks like this:

Code:
<%@ Page Language="C#" MasterPageFile="~/MasterPages/ManagementMaster.master" AutoEventWireup="true" CodeFile="AddEditReview.aspx.cs" Inherits="Management_AddEditReview" Title="Planet Wrox - Management - Insert and Update Reviews" %>
<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="LinqDataSource1" DefaultMode="Insert" 
        Height="50px" Width="125px" oniteminserted="DetailsView1_ItemInserted" 
        oniteminserting="DetailsView1_ItemInserting" 
        onitemupdated="DetailsView1_ItemUpdated" 
        onitemupdating="DetailsView1_ItemUpdating">
        <Fields>
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
            <asp:TemplateField HeaderText="Title" SortExpression="Title">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqVal1" ControlToValidate="TextBox1" 
                                 runat="server" ErrorMessage="Please enter a title">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqVal2" ControlToValidate="TextBox1" 
                                 runat="server" ErrorMessage="Please enter a title">
                    </asp:RequiredFieldValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Summary" SortExpression="Summary">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Summary") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" TextMode="MultiLine" Width="500" Height="100" runat="server"
                                             Text='<%# Bind("Summary") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqVal3" ControlToValidate="TextBox2" 
                                 runat="server" ErrorMessage="Please enter a summary">
                    </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2" TextMode="MultiLine" Width="500" Height="100" runat="server"
                                             Text='<%# Bind("Summary") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqVal4" ControlToValidate="TextBox2" 
                                 runat="server" ErrorMessage="Please enter a summary">
                    </asp:RequiredFieldValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Body" SortExpression="Body">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Body") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" TextMode="MultiLine" Width="500" Height="100" runat="server" Text='<%# Bind("Body") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="GenreId" SortExpression="GenreId">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("GenreId") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsGenres" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("GenreId") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsGenres" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("GenreId") %>'>
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:CheckBoxField DataField="Authorized" HeaderText="Authorized" SortExpression="Authorized" />
            <asp:BoundField DataField="UpdateDateTime" HeaderText="UpdateDateTime" SortExpression="UpdateDateTime" Visible="False" />
            <asp:TemplateField HeaderText="PhotoAlbumId" SortExpression="PhotoAlbumId">
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Eval("PhotoAlbumId") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" 
                        DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id" 
                        SelectedValue='<%# Bind("PhotoAlbumId") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" 
                        DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id" 
                        SelectedValue='<%# Bind("PhotoAlbumId") %>'>
                    </asp:DropDownList>
                    
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
        ContextTypeName="PlanetWroxDataContext" TableName="Reviews" 
        Where="Id == @Id" 
        Select="new (Title, Summary, Body, GenreId, PhotoAlbumId, Authorized, UpdateDateTime)">
        <WhereParameters>
            <asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" DefaultValue="0"/>
        </WhereParameters>
    </asp:LinqDataSource>
    
    <asp:SqlDataSource ID="sdsGenres" runat="server" ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>" SelectCommand="SELECT [Id], [Name] FROM [Genre] ORDER BY [SortOrder]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:PlanetWroxConnectionString1 %>" 
                        SelectCommand="SELECT [Id], [Name] FROM [PhotoAlbum]">
    </asp:SqlDataSource>
</asp:Content>
Any suggestions what could cause this?

Thanks, Robin
 
Old December 12th, 2009, 07:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Robin,

I don't see anything obviously wrong with this code. However, it's quite possible we're overlooking something. Difficult for me to see with just the code for this page, and no possibility to test it out.

Can you send me a zipped version of your project so I can test it out?

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 December 13th, 2009, 05:51 AM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default

Ok, I'll do that. Thanks for having a look at it.

Robin
 
Old December 13th, 2009, 12:55 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, got it. And replied to your private e-mail.

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!
The Following User Says Thank You to Imar For This Useful Post:
robbaralla (December 13th, 2009)
 
Old December 13th, 2009, 04:20 PM
Authorized User
 
Join Date: Apr 2009
Posts: 48
Thanks: 16
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for the to the point answer. I thought I'd put the answer on the forum in case anybody else might be interested.
I just discovered that in my code, also listed here above the ' EnableInsert="True" ' is missing. Even with this in place however it still does not work.

So that is where Imar's line comes in, in the code behind file :

e.Values["CreateDateTime"] = DateTime.Now;

in the DetailsView1_Inserting eventhandler.

This code makes the insert work, even though on p376 for the CreatDateTime column a Default Value was set already in the dataBase itself...

Strangely enough this is ignored....

regards, Robin
 
Old December 14th, 2009, 11:51 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Thanks for sharing such innovative idea.
And also thanks goes to Imar sir who made the stuff working.
Thanks both of you....





Similar Threads
Thread Thread Starter Forum Replies Last Post
PhotoAlbum! ostwald BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 18 October 23rd, 2007 03:11 PM
PhotoAlbum with MS Access cwh BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 March 16th, 2007 03:05 AM
Is anyone converted PhotoAlbum in Csharp ravi.gurgaon BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 March 2nd, 2007 02:11 PM
problem in adding new photo in PhotoAlbum ravi.gurgaon BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 December 13th, 2006 02:31 PM
difference between include file & include virtual crmpicco Classic ASP Basics 2 January 23rd, 2006 11:50 AM





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