C# 导致ASP网站异常的DELETE语句
不知道为什么这不起作用,正在寻求帮助。我有一个ASP网站,我正在建设。一个页面设置为主控/详细信息页面,其中GridView用于显示表中的记录。带有INSERT、UPDATE和DELETE语句的DetailView绑定到我的GridView上的SELECT按钮 INSERT和UPDATE语句工作正常,但DELETE语句引发异常C# 导致ASP网站异常的DELETE语句,c#,asp.net,sql-server,detailsview,C#,Asp.net,Sql Server,Detailsview,不知道为什么这不起作用,正在寻求帮助。我有一个ASP网站,我正在建设。一个页面设置为主控/详细信息页面,其中GridView用于显示表中的记录。带有INSERT、UPDATE和DELETE语句的DetailView绑定到我的GridView上的SELECT按钮 INSERT和UPDATE语句工作正常,但DELETE语句引发异常 protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
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();
}
下面是处理异常的代码
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();
}
protectedvoid DetailsView1\u itemsdeleted(对象发送方,DetailsViewDeletedEventArgs e)
{
如果(例如异常!=null)
{
lblError.Text=“发生数据库错误。
“+”消息:“+e.Exception.Message;
e、 ExceptionHandled=true;
}
else if(e.AffectedRows==0)
lblError.Text=“其他用户可能已更新该产品。”+“
请重试。”;
其他的
GridView1.DataBind();
}
引发的异常是e.AffectedRows异常
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();
}
我不知道为什么我的桌子不接受删除声明,任何想法将不胜感激。下面是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>
通常在删除(
ProductID
?)时,我只会使用我的where
子句中的主键。delete语句可能在where条件下失败
如果没有与delete命令相关的业务规则,您可以尝试以下操作:
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @original_ProductID"
通常,在删除(
ProductID
?)时,我只使用where
子句中的主键。delete语句可能在where条件下失败
如果没有与delete命令相关的业务规则,您可以尝试以下操作:
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @original_ProductID"
你能先排除db的问题吗?ie:你能直接通过SQL运行delete命令吗?这里实际的堆栈跟踪会更有帮助。但是,正如Kyle C所提到的,根据数据库验证查询是第一步……如果ProductID是唯一的,您可以简化DELETE和UPDATE中的WHERE子句。@Brian我认为不会有堆栈stace,因为affectedrows是0。因此,删除条件与任何记录都不匹配。@jrummell OP声明抛出的异常是e.AffectedRows异常。但是
e.AffectedRows
不是异常。它到达了代码的这一部分这一事实意味着e.Exception=null
您可以先排除db的问题吗?ie:你能直接通过SQL运行delete命令吗?这里实际的堆栈跟踪会更有帮助。但是,正如Kyle C所提到的,根据数据库验证查询是第一步……如果ProductID是唯一的,您可以简化DELETE和UPDATE中的WHERE子句。@Brian我认为不会有堆栈stace,因为affectedrows是0。因此,删除条件与任何记录都不匹配。@jrummell OP声明抛出的异常是e.AffectedRows异常。但是e.AffectedRows
不是异常。它到达了代码的这一部分,这意味着e.Exception=null
感谢您的回答,问题发生在WHERE子句的某个地方。仍然不确定原因…但是您的建议证明了delete命令只通过指定主键起作用。最后,我从头开始重新写了这一页,第二次是一种魅力。作为一个noob,他不能说为什么第一次不起作用(第二次尝试的代码看起来是一样的),但是它确实起作用了,你的建议帮助缩小了范围。谢谢大家的建议。谢谢你的回答,问题发生在WHERE子句的某个地方。仍然不确定原因…但是您的建议证明了delete命令只通过指定主键起作用。最后,我从头开始重新写了这一页,第二次是一种魅力。作为一个noob,他不能说为什么第一次不起作用(第二次尝试的代码看起来是一样的),但是它确实起作用了,你的建议帮助缩小了范围。谢谢大家的建议。