简体   繁体   English

IDENTITY_INSERT设置为OFF

[英]IDENTITY_INSERT is set to OFF

<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: SQL Server表结构:

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. 我有一个正在显示一些数据的gridview,我知道希望通过详细信息视图输入一些新行。 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. 当IDENTITY_INSERT设置为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 ! 任何帮助或建议都很好!

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 错误的原因是由于您试图将值插入到具有标识列的表中而SQL建议,如果要这样做,则必须启用

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IDENTITY_INSERT 设置为 OFF 的问题? :-/ - Problems with IDENTITY_INSERT Set to OFF? :-/ IDENTITY_INSERT 设置为 OFF - Visual Studio - IDENTITY_INSERT is set to OFF - Visual Studio IDENTITY_INSERT设置为OFF时,无法手动为标识插入ID实体插入显式值 - Cannot insert explicit value for identity insert ID manually entity when IDENTITY_INSERT is set to OFF 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表&#39;[table]&#39;中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为表&#39;ActivityInstances中的标识列插入显式值 - Cannot insert explicit value for identity column in table 'ActivityInstances' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为标识列插入显式值 - Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;Recipe&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Recipe' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在表&#39;MyTableName&#39;中为标识列插入显式值 - Cannot insert explicit value for identity column in table 'MyTableName' when IDENTITY_INSERT is set to OFF SqlException:当IDENTITY_INSERT设置为OFF时,无法为表&#39;AspNetUsers&#39;中的Identity列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'AspNetUsers' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在表&#39;candidatedetails&#39;中为identity列插入显式值 - Cannot insert explicit value for identity column in table 'candidatedetails' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM