不确定为什么这不起作用并寻求任何帮助。我有一个ASP网站,我正在建设。一个页面设置为主/明细页面,其中GridView用于显示表格中的记录。带有INSERT,UPDATE和DELETE STATEMENTS的DetailView绑定到GridView上的SELECT按钮。
INSERT和UPDATE语句没有问题,但DELETE语句抛出异常。
以下是处理异常的代码。
protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = "A database error has occurred.<br /><br />" + "Message: " + e.Exception.Message;
e.ExceptionHandled = true;
}
else if (e.AffectedRows == 0)
lblError.Text = "Another user may have updated that product." + "<br />Please try again.";
else
GridView1.DataBind();
}
抛出的异常是e.AffectedRows Exception。
我不确定为什么我的桌子不接受DELETE声明,任何想法都会受到赞赏。这是DetailView的代码
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:HalloweenConnectionString %>"
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @original_ProductID AND [Name] = @original_Name AND [ShortDescription] = @original_ShortDescription AND [LongDescription] = @original_LongDescription AND [CategoryID] = @original_CategoryID AND (([ImageFile] = @original_ImageFile) OR ([ImageFile] IS NULL AND @original_ImageFile IS NULL)) AND [UnitPrice] = @original_UnitPrice AND [OnHand] = @original_OnHand"
InsertCommand="INSERT INTO [Products] ([ProductID], [Name], [ShortDescription], [LongDescription], [CategoryID], [ImageFile], [UnitPrice], [OnHand]) VALUES (@ProductID, @Name, @ShortDescription, @LongDescription, @CategoryID, @ImageFile, @UnitPrice, @OnHand)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [ProductID], [Name], [ShortDescription], [LongDescription], [CategoryID], [ImageFile], [UnitPrice], [OnHand] FROM [Products] WHERE ([ProductID] = @ProductID)"
UpdateCommand="UPDATE [Products] SET [Name] = @Name, [ShortDescription] = @ShortDescription, [LongDescription] = @LongDescription, [CategoryID] = @CategoryID, [ImageFile] = @ImageFile, [UnitPrice] = @UnitPrice, [OnHand] = @OnHand WHERE [ProductID] = @original_ProductID AND [Name] = @original_Name AND [ShortDescription] = @original_ShortDescription AND [LongDescription] = @original_LongDescription AND [CategoryID] = @original_CategoryID AND (([ImageFile] = @original_ImageFile) OR ([ImageFile] IS NULL AND @original_ImageFile IS NULL)) AND [UnitPrice] = @original_UnitPrice AND [OnHand] = @original_OnHand">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="ProductID"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="original_ProductID" Type="String" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_ShortDescription" Type="String" />
<asp:Parameter Name="original_LongDescription" Type="String" />
<asp:Parameter Name="original_CategoryID" Type="String" />
<asp:Parameter Name="original_ImageFile" Type="String" />
<asp:Parameter Name="original_UnitPrice" Type="Decimal" />
<asp:Parameter Name="original_OnHand" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="ShortDescription" Type="String" />
<asp:Parameter Name="LongDescription" Type="String" />
<asp:Parameter Name="CategoryID" Type="String" />
<asp:Parameter Name="ImageFile" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
<asp:Parameter Name="OnHand" Type="Int32" />
<asp:Parameter Name="original_ProductID" Type="String" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_ShortDescription" Type="String" />
<asp:Parameter Name="original_LongDescription" Type="String" />
<asp:Parameter Name="original_CategoryID" Type="String" />
<asp:Parameter Name="original_ImageFile" Type="String" />
<asp:Parameter Name="original_UnitPrice" Type="Decimal" />
<asp:Parameter Name="original_OnHand" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductID" Type="String" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="ShortDescription" Type="String" />
<asp:Parameter Name="LongDescription" Type="String" />
<asp:Parameter Name="CategoryID" Type="String" />
<asp:Parameter Name="ImageFile" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Decimal" />
<asp:Parameter Name="OnHand" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
答案 0 :(得分:1)
通常我在删除where
时只使用ProductID
子句中的主键?删除语句可能在where条件中失败。
如果没有与delete命令相关的业务规则,可以试试这个:
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @original_ProductID"