我正在尝试更新具有和indentity列的表。因此,当我尝试更新gridview中的一行时,我收到以下错误“无法更新标识列'ManufactureID'。”“我知道您可以插入更新的行,然后删除旧行。
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Name" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="ManufactureID" HeaderText="ManufactureID" ReadOnly="True" SortExpression="ManufactureID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="Providence" HeaderText="Providence" SortExpression="Providence" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="ZipCode" HeaderText="ZipCode" SortExpression="ZipCode" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString1 %>" DeleteCommand="DELETE FROM [Manufacturer] WHERE [Name] = @Name" InsertCommand="INSERT INTO [Manufacturer] ([Name], [Address], [Providence], [City], [ZipCode], [Country], [Phone], [Fax], [Email]) VALUES (@Name, @Address, @Providence, @City, @ZipCode, @Country, @Phone, @Fax, @Email)" ProviderName="<%$ ConnectionStrings:SQLTestConnectionString1.ProviderName %>" SelectCommand="SELECT [ManufactureID], [Name], [Address], [Providence], [City], [ZipCode], [Country], [Phone], [Fax], [Email] FROM [Manufacturer]" UpdateCommand="UPDATE [Manufacturer] SET [ManufactureID] = @ManufactureID, [Address] = @Address, [Providence] = @Providence, [City] = @City, [ZipCode] = @ZipCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax, [Email] = @Email WHERE [Name] = @Name">
<DeleteParameters>
<asp:Parameter Name="Name" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Providence" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
<asp:Parameter Name="Email" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ManufactureID" Type="Int32" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Providence" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="ZipCode" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Fax" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Name" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
答案 0 :(得分:0)
UpdateCommand
标记的<asp:SqlDataSource>
属性中指定的语句会尝试设置[ManufactureID],这就是您收到错误的原因:
UpdateCommand="UPDATE [Manufacturer] SET [ManufactureID] = @ManufactureID, [Address] = @Address, [Providence] = @Providence, [City] = @City, [ZipCode] = @ZipCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax, [Email] = @Email WHERE [Name] = @Name"
您需要移除[ManufactureID] = @ManufactureID,
并从<asp:Parameter Name="ManufactureID" Type="Int32" />
下删除<UpdateParameters>
,这应该可以解决问题。