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