嘿,我只是在学习如何使用 asp.net,我正在尝试使用网格视图来显示信息和详细信息视图,以便从数据库中编辑/删除信息。现在我可以编辑和更新数据库中的信息,但我也希望将其删除,但有些东西对我不起作用。我设置了一个例外,当我选择该项目并按删除时,它会弹出我的例外,即另一个用户删除了该项目,而它没有。我不确定我做错了什么,对我来说它看起来应该有效。
这是详细信息视图中删除命令和参数的代码
DeleteCommand="DELETE FROM [Customers]
WHERE [CustomerID] = @original_CustomerID
AND [Name] = @original_Name
AND [Address] = @original_Address
AND [City] = @original_City
AND [State] = @original_State
AND [ZipCode] = @original_ZipCode
AND ([Phone] = @original_Phone
OR [Phone] IS NULL AND @original_Phone IS NULL)
AND ([Email] = @original_Email
OR [Email] IS NULL AND @original_Email IS NULL)"
<DeleteParameters>
<asp:Parameter Name="original_CustomerID" Type="Int32" />
<asp:Parameter Name="original_Name" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
<asp:Parameter Name="original_City" Type="String" />
<asp:Parameter Name="original_State" Type="String" />
<asp:Parameter Name="original_ZipCode" Type="String" />
<asp:Parameter Name="original_Phone" Type="String" />
<asp:Parameter Name="original_Email" Type="String" />
</DeleteParameters>
这是cs文件中的删除功能
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 deleted that customer."
+ "<br />Please try again.";
else
GridView1.DataBind();
}
protected void DetailsView1_ItemDeleting(
object sender, DetailsViewDeleteEventArgs e)
{
e.Values["Name"]
= e.Values["Name"].ToString().Substring(1);
}