Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
|
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : 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 March 22nd, 2014, 06:43 PM
Registered User
 
Join Date: Mar 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cannot insert explicit value for identity column in table 'PhotoAlbumTable' when IDEN

I've been trying to figure out how to fix this for 4 hours now,.

I have my table's primary keys set to is identity YES for automatic assignment of a unique number, yet I'm still having this problem here's a part of my code:

HTML Code:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="EntityDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
        <Fields>
            <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" InsertVisible="false"/>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"  />
            <asp:CommandField ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=AnimeCoffeeEntities" DefaultContainerName="AnimeCoffeeEntities" EnableFlattening="False" EntitySetName="PhotoAlbumTables" OnInserted="EntityDataSource1_Inserted" EnableInsert="True">
    </asp:EntityDataSource>
Here's my codebehind:

Code:
 protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (e.Entity != null)
        {
            PhotoAlbumTable myPhotoAlbum = (PhotoAlbumTable)e.Entity;
            Response.Redirect(string.Format("~/Demos/ManagePhotoAlbum.aspx?PhotoAlbumID={0}", myPhotoAlbum.ID.ToString()));
        }
    }
By the way the naming of my tables are modified a little bit in a way that I can understand it better, like the PhotoAlbum to PhotoAlbumTable.

Thanks,
 
Old March 22nd, 2014, 06:51 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you provide the *full* code (markup and Code Behind) of both pages? Also, can you post the full URL from the browser's address bar for the page that raises the error?

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 March 22nd, 2014, 07:46 PM
Registered User
 
Join Date: Mar 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's the full aspx file:
HTML Code:
<%@ Page Title="Create Photo Album" Language="C#" MasterPageFile="~/MasterPages/Thread.master" AutoEventWireup="true" CodeFile="NewPhotoAlbum.aspx.cs" Inherits="Demos_NewPhotoAlbum" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Thread" Runat="Server">

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="EntityDataSource1" DefaultMode="Insert" Height="50px" Width="125px">
        <Fields>
            <%--<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" InsertVisible="false"/>--%>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"  />
            <asp:CommandField ShowInsertButton="true" />
        </Fields>
    </asp:DetailsView>
    <asp:EntityDataSource ID="EntityDataSource1" EnableInsert="true" runat="server" ConnectionString="name=AnimeCoffeeEntities" DefaultContainerName="AnimeCoffeeEntities" EnableFlattening="False" EntitySetName="PhotoAlbumTables" OnInserted="EntityDataSource1_Inserted">
    </asp:EntityDataSource>

</asp:Content>
Here's the codebehind:
HTML Code:
using AnimeCoffeeModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void EntityDataSource1_Inserted(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (e.Entity != null)
        {
            PhotoAlbumTable myPhotoAlbum = (PhotoAlbumTable)e.Entity;
            Response.Redirect(string.Format("~/Demos/ManagePhotoAlbum.aspx?PhotoAlbumID={3}", myPhotoAlbum.ID.ToString()));



        }
    }
}
Here's the browser link on error:
http://localhost:49261/Demos/NewPhotoAlbum.aspx

also here's the full error:
Cannot insert explicit value for identity column in table 'PhotoAlbumTable' when IDENTITY_INSERT is set to OFF.

AnimeCoffee is the name of my mdf file by the way and my masterpage is nested from SuperPage.master(for header,nav, footer) to a masterpage for specific webforms, with this one it is Thread.master So it's like SuperPage.master->Thread.master

Last edited by Katsune; March 22nd, 2014 at 07:52 PM..
 
Old March 23rd, 2014, 08: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

Did you set the Identity for the Id column before you created the EF diagram? Looks like EF thinks it needs to supply an explicit value. If you set the identity afterwards, go to the EF diagram, right-click and choose Update from database and then try again.

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 March 23rd, 2014, 10:22 AM
Registered User
 
Join Date: Mar 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
Did you set the Identity for the Id column before you created the EF diagram? Looks like EF thinks it needs to supply an explicit value. If you set the identity afterwards, go to the EF diagram, right-click and choose Update from database and then try again.

Cheers,

Imar
Holy beast,... that was quick, it worked,.. I never tried to touch the diagram because I was afraid of ruining my relations..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13 - Cannot insert explicit value for identity column in table 'Picture' when hahoa BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 10 November 6th, 2009 01:29 PM
RETURN IDENTITY AFTER INSERT INTO SECOND TABLE pallone SQL Language 2 August 19th, 2006 05:51 PM
getting identity column from the table g_vamsi_krish SQL Server 2000 1 March 15th, 2006 05:05 PM
Alter a column as identity for table kasanar SQL Server 2000 5 July 6th, 2005 08:44 AM





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