Reputation:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="table table-striped" DataKeyNames="productsID" DataSourceID="productsObj">
<Columns>
<asp:BoundField DataField="productsID" HeaderText="productsID" ReadOnly="True" SortExpression="productsID" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
<asp:BoundField DataField="image" HeaderText="image" SortExpression="image" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="productsObj" runat="server"
DeleteMethod="Delete" InsertMethod="Insert"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData"
TypeName="productsTableAdapters.productsTableAdapter"
UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_productsID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="productsID" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="price" Type="Decimal" />
<asp:Parameter Name="image" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="price" Type="Decimal" />
<asp:Parameter Name="image" Type="String" />
<asp:Parameter Name="Original_productsID" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
<div class="col-lg-10 col-lg-offset-1 col-md-12">
<div class="col-lg-6">
<asp:Label ID="Label1" CssClass="h1" runat="server" Text="Add Albums"></asp:Label>
<asp:DetailsView ID="DetailsView1" runat="server" CssClass="table table-striped" AutoGenerateRows="False" DataKeyNames="productsID" DataSourceID="productsObj" DefaultMode="Insert">
<Fields>
<asp:BoundField DataField="productsID" HeaderText="productsID" ReadOnly="True" SortExpression="productsID" InsertVisible="False" />
<asp:BoundField DataField="name" HeaderText="name" ControlStyle-CssClass="input-sm form-control" SortExpression="name" />
<asp:BoundField DataField="price" HeaderText="price" ControlStyle-CssClass=" input-sm form-control" SortExpression="price" />
<asp:BoundField DataField="image" HeaderText="image" ControlStyle-CssClass=" input-sm form-control" SortExpression="image" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
</div>
SQL Server table structure:
CREATE TABLE [dbo].[products]
(
[productsID] INT IDENTITY (1, 1) NOT NULL,
[name] VARCHAR(50) NOT NULL,
[price] DECIMAL(18, 2) NOT NULL,
[image] VARCHAR(255) NOT NULL,
PRIMARY KEY CLUSTERED ([productsID] ASC)
);
I have a gridview which is displaying some data, I am know wishing to enter in some new rows via the details view. For some odd reason I keep getting this error:
Cannot insert explicit value for identity column in table 'products' when IDENTITY_INSERT is set to OFF.
I really don't know what it is causing it to do this, so I'm stuck to for a solution. Any help or advice would be great !
Upvotes: 0
Views: 955
Reputation: 28940
The reason for error is due to the fact that, you are trying to insert values into a table with identity column and SQL is suggesting,if you want to do that,you have to enable
SET IDENTITY_INSERT ON;
if you don't want insert values into identity column, you can do below
insert into table
(all columns except identity)
values
(col values for all except identity)
Upvotes: 1