In the third task I get the error when creating the album.
Code:
Server Error in '/' Application.
Inserting is not supported unless the InsertMethod is specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: Inserting is not supported unless the InsertMethod is specified.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NotSupportedException: Inserting is not supported unless the InsertMethod is specified.]
System.Web.UI.WebControls.ModelDataSourceView.EvaluateInsertMethodParameters(IDictionary values, ModelDataSourceMethod method) +2455237
System.Web.UI.WebControls.ModelDataSourceView.EvaluateInsertMethodParameters(IDictionary values) +11
System.Web.UI.WebControls.ModelDataSourceView.GetInsertMethodResult(IDictionary values, ModelDataSourceMethod method, Boolean isAsyncMethod, Nullable`1 cancellationToken) +39
System.Web.UI.WebControls.ModelDataSourceView.GetInsertMethodResult(IDictionary values) +41
System.Web.UI.WebControls.ModelDataSourceView.ExecuteInsert(IDictionary values) +20
System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +100
System.Web.UI.WebControls.ModelDataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +140
System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +365
System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +621
System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +89
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +82
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +161
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9654230
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
I checked both files NewPhotoAlbum.aspx and NewPhotoAlbum.cs with the source, everything is correct.
Code:
<%@ Page Title="Create New Photo Album" Language="C#" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" Inherits="NewPhotoAlbum" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" runat="Server">
<asp:DetailsView AutoGenerateRows="false" ID="DetailsView1" runat="server" DefaultMode="Insert" SelectMethod="DetailsView1_InsertItem">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:CommandField ShowInsertButton="True" ShowCancelButton="false" />
</Fields>
</asp:DetailsView>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="cpClientScript" runat="Server">
</asp:Content>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class NewPhotoAlbum : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void DetailsView1_InsertItem()
{
PhotoAlbum photoAlbum = new PhotoAlbum();
TryUpdateModel(photoAlbum);
if (ModelState.IsValid)
{
using (var myEntities = new PlanetWroxTempEntities())
{
myEntities.PhotoAlbums.Add(photoAlbum);
myEntities.SaveChanges();
}
Response.Redirect(string.Format("ManagePhotoAlbum?PhotoAlbumId={0}", photoAlbum.Id.ToString()));
}
}
}
Strange as that IntellySense automatically creates an event "DetailsView1_GetItem" and not "DetailsView1_InsertItem", despite the fact that specified DefaultMode="Insert". I fixed a "Get" on "Insert" in the manual
Code