无法更新标识列

时间:2017-11-14 14:45:51

标签: sql asp.net visual-studio datagridview

我想编辑数据库中的数据,但如果我尝试编辑行中的任何值,则会显示错误消息

  

无法更新标识列' ProductID'

尝试更新数据后出现

。我只是一个初学者,所以我不知道bug在哪里。

<asp:SqlDataSource ID="Adventure" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConStrAdventure %>" 
     DeleteCommand="DELETE FROM [Production].[Product] WHERE [ProductID] = @ProductID" 
     InsertCommand="INSERT INTO [Production].[Product]  ([ProductID], [Name], [ProductNumber], [MakeFlag], [FinishedGoodFlag], [Color], [SafetyStockLevel], [ReorderPoint], [StandardCost], [ListPrice], [Size], [SizeUnitMeasureCode], [WeightUnitMeasureCode], [Weight], [DaysToManufacture], [ProductLine], [Class], [Style], [ProductSubcategoryID], [ProductModelID], [SellStartDate], [SellEndDate], [DiscontinuedDate], [rowguid], [ModifiedDate]) VALUES (@ProductID, @Name, @ProductNumber, @MakeFlag, @FinishedGoodFlag, @Color, @SafetyStockLevel, @ReorderPoint, @StandardCost, @ListPrice, @Size, @SizeUnitMeasureCode, @WeightUnitMeasureCode, @Weight, @DaysToManufacture, @ProductLine, @Class, @Style, @ProductSubcategoryID, @ProductModelID, @SellStartDate, @SellEndDate, @DiscontinuedDate, @rowguid, @ModifiedDate) " SelectCommand="SELECT * FROM [Production].[Product]" 
     UpdateCommand="UPDATE [Production].[Product] SET [ProductID] = @ProductID,[Name] = @Name, [ProductNumber] = @ProductNumber, [MakeFlag] = @MakeFlag, [FinishedGoodsFlag] = @FinishedGoodsFlag, [Color] = @Color, [SafetyStockLevel] = @SafetyStockLevel, [ReorderPoint] = @ReorderPoint, [StandardCost] = @StandardCost, [ListPrice] = @ListPrice, [Size] = @Size, [SizeUnitMeasureCode] = @SizeUnitMeasureCode, [WeightUnitMeasureCode] = @WeightUnitMeasureCode, [Weight] = @Weight, [DaysToManufacture] =  @DaysToManufacture, [ProductLine] = @ProductLine, [Class] = @Class, [Style] = @Style, [ProductSubcategoryID] = @ProductSubcategoryID, [ProductModelID] = @ProductModelID, [SellStartDate] = @SellStartDate, [SellEndDate] = @SellEndDate, [DiscontinuedDate] = @DiscontinuedDate, [rowguid] = @rowguid, [ModifiedDate] = @ModifiedDate WHERE [ProductID] = @ProductID">

The second part of the code here as image.

提前感谢您的任何想法。

1 个答案:

答案 0 :(得分:1)

您的更新命令开始,

UpdateCommand="UPDATE [Production].[Product] SET [ProductID] = @ProductID,[Name],.....

错误消息告诉您无法更新[ProductID],因为它是 &#39;标识栏&#39; ,这是你的主要关键。您无法更新主键,这是识别记录的原因。只需从[ProductID] = @ProductID中的列/数据对列表中删除UpdateCommand