I am trying to update a table that has and indentity column. So when I try to update a row in gridview I get the following error "Cannot update identity column 'ManufactureID'.]" I know you can insert the updated row and then delete the old row.
<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>
The statement specified in the UpdateCommand
attribute of your <asp:SqlDataSource>
tag tries to set the [ManufactureID], that's why you're getting the error:
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"
You need to remove [ManufactureID] = @ManufactureID,
and also remove <asp:Parameter Name="ManufactureID" Type="Int32" />
from under <UpdateParameters>
, that should do the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.