Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

ASP.NET
<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>


and here's my code behind:
C#
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()));
        }
    }
Posted
Comments
CHill60 22-Mar-14 14:35pm    
Does this help ? ASP.net detailsview Insert[^]
KatsuneShinsengumi 22-Mar-14 17:52pm    
I read it Chill, thanks, I'm not directly touching sql server, I'm using Entity Framework as a medium tool between the asp controls and the SQL Server, I find it to be more efficient although I'm still having trouble to understand the full concept of how it works, just like this problem.
Nirav Prabtani 22-Mar-14 14:40pm    
what is problame??
KatsuneShinsengumi 22-Mar-14 17:50pm    
The problem is my Thread title, whenever I run this code it post that exception in the browser,. man it's already 6am,.

You're inserting values for the identity column. But you have turned on identity insert on that column.

Instead turn on identity insert on the table like this so that you can specify your own identity values.

SQL
SET IDENTITY_INSERT PhotoAlbumTable ON

INSERT INTO PhotoAlbumTables
/*Note the column list is REQUIRED here, not optional*/
            (ID, Name)
VALUES      (20, 'John')

SET IDENTITY_INSERT PhotoAlbumTable OFF 


[Please accept/up-vote answers or solutions that work for you to encourage participation]
 
Share this answer
 
Comments
KatsuneShinsengumi 22-Mar-14 17:58pm    
Can you verify what part of my code does the insertion of a value in the identity column that is already set to be pre inserted when the row is filled with data?
CoderPanda 23-Mar-14 1:41am    
The DetailsView control that you are using has the DefaultMode set to Insert. I think that is where, when you save, it tries to insert the data into the dataset.
Hey Guys I got it now,

It happened because I made my Primary keys set to is Identity AFTER I made an Entity FrameWork Diagram to hook it in my controls, so the Entity FrameWork Diagram is not updated with who are the "is Identity" columns. After updating the diagram magically everything worked fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900