导致 ASP 网站异常的 DELETE 语句

本文关键字:DELETE 语句 异常 ASP 网站 导致 | 更新日期: 2023-09-27 17:56:45

不知道为什么这不起作用并寻求任何帮助。我有一个正在构建的 ASP 网站。一个页面设置为大纲/详细信息页面,其中 GridView 用于显示表中的记录。具有插入、更新和删除语句的详细信息视图绑定到我的 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.受影响的行异常。

我不确定为什么我的表不接受 DELETE 语句,任何想法将不胜感激。这是详细视图的代码

<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>

导致 ASP 网站异常的 DELETE 语句

通常我在删除时只使用where子句中的主键(ProductID?删除语句在 where 条件下可能会失败。

如果没有与删除命令相关的业务规则,您可以尝试以下操作:

DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @original_ProductID"