我的页面上有一个gridview控件,每当我尝试使用它附带的删除按钮时,它就会给我上面提到的错误。这是我的代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="AccessDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:ImageField DataImageUrlField="picPath"
DataImageUrlFormatString="PlaceImages/{0}" HeaderText="Picture" ControlStyle-CssClass="editPhotoGridFormat">
<ControlStyle CssClass="editPhotoGridFormat"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="PicPath" HeaderText="Filename" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/TravelJoansDB.mdb"
DeleteCommand="DELETE FROM [Slides] WHERE [ID] = ?"
InsertCommand="INSERT INTO [Slides] ([ID], [PicPath], [Title], [City], [Country]) VALUES (?, ?, ?, ?, ?)"
SelectCommand="SELECT * FROM [Slides]"
UpdateCommand="UPDATE [Slides] SET [PicPath] = ?, [Title] = ?, [City] = ?, [Country] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="PicPath" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Country" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="PicPath" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Country" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
</asp:AccessDataSource>
我实际上遇到了这个问题,另一个页面上有更多东西,而不仅仅是gridview。我很好奇,如果它与它有任何关系,所以我只是创建了一个新的页面,只复制了数据源和gridview,它仍然给了我同样的错误。表中只有9条记录,肯定没有重复的PK。只有ID字段被标记为表中的主键。发生了什么事?
答案 0 :(得分:0)
请勿使用命令中提到的问号。请按照下面的说法进行更改,然后就可以了。
DeleteCommand="DELETE FROM [Slides] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [Slides] ([ID], [PicPath], [Title], [City], [Country]) VALUES (@ID, @PicPath, @Title, @City, @Country)"
SelectCommand="SELECT * FROM [Slides]"
UpdateCommand="UPDATE [Slides] SET [PicPath] = @PicPath, [Title] = @Title, [City] = @City, [Country] = @Country WHERE [ID] = @ID">
如果你的表ID有自动增量,那么不要在insert命令中使用ID。
DeleteCommand="DELETE FROM [Slides] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [Slides] ([PicPath], [Title], [City], [Country]) VALUES ( @PicPath, @Title, @City, @Country)"
SelectCommand="SELECT * FROM [Slides]"
UpdateCommand="UPDATE [Slides] SET [PicPath] = @PicPath, [Title] = @Title, [City] = @City, [Country] = @Country WHERE [ID] = @ID">