从 GridView 中删除时出错
Error deleting from GridView
在我的 GridView 中,我有一个员工列表。当我单击删除 link 时,我正在调用一个存储过程,该过程从两个 table 中删除信息。第一个 table 无论如何都可以正常工作。第二个 table 也工作正常,但是,如果最初有超过 4 行数据与给定的 EmployeeID 对应,它 returns 一个错误,即使所有数据都按预期删除。如果我删除第二个删除语句并保留 Cities table 原样,我不会收到任何错误,因此它一定与该命令有关。由于来自第二个 table 的数据没有出现在 GridView 中,我不确定为什么会这样。有什么想法吗?
存储过程:删除用户
DELETE FROM Users WHERE ID=@ID
DELETE FROM Cities WHERE ID=@ID
ASPX
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDS1" AllowSorting="True">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" ReadOnly="True" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" ReadOnly="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDS1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="SelectUser"
DeleteCommandType="StoredProcedure" DeleteCommand="DeleteUser">
<SelectParameters>
<asp:Parameter Name="ID" Type="String" DefaultValue="All" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ID" Type="String"></asp:Parameter>
</DeleteParameters>
</asp:SqlDataSource>
错误
Specified argument was out of the range of valid values.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value]
System.Web.UI.WebControls.GridView.set_SelectedIndex(Int32 value) +1350923
System.Web.UI.WebControls.GridView.HandleDeleteCallback(Int32 affectedRows, Exception ex) +368
System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +137
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +714
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +869
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +207
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
此外,我应该提到,如果我直接在 SQL 中 运行 DeleteUser 存储过程,所有内容都会按预期从两个 table 中删除,并且不会返回任何错误。
我在另一个论坛上遇到了 post,有人报告说设置 AllowPaging="True" 解决了这个问题。我不确定为什么这很重要,尽管我继续做了同样的事情并且我的问题也得到了解决。我很想知道为什么这解决了这个问题,但至少它现在可以工作了。
在我的 GridView 中,我有一个员工列表。当我单击删除 link 时,我正在调用一个存储过程,该过程从两个 table 中删除信息。第一个 table 无论如何都可以正常工作。第二个 table 也工作正常,但是,如果最初有超过 4 行数据与给定的 EmployeeID 对应,它 returns 一个错误,即使所有数据都按预期删除。如果我删除第二个删除语句并保留 Cities table 原样,我不会收到任何错误,因此它一定与该命令有关。由于来自第二个 table 的数据没有出现在 GridView 中,我不确定为什么会这样。有什么想法吗?
存储过程:删除用户
DELETE FROM Users WHERE ID=@ID
DELETE FROM Cities WHERE ID=@ID
ASPX
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDS1" AllowSorting="True">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" ReadOnly="True" />
<asp:BoundField DataField="Username" HeaderText="Username"
SortExpression="Username" ReadOnly="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDS1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="SelectUser"
DeleteCommandType="StoredProcedure" DeleteCommand="DeleteUser">
<SelectParameters>
<asp:Parameter Name="ID" Type="String" DefaultValue="All" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ID" Type="String"></asp:Parameter>
</DeleteParameters>
</asp:SqlDataSource>
错误
Specified argument was out of the range of valid values.
Parameter name: value
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value]
System.Web.UI.WebControls.GridView.set_SelectedIndex(Int32 value) +1350923
System.Web.UI.WebControls.GridView.HandleDeleteCallback(Int32 affectedRows, Exception ex) +368
System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +137
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +714
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +869
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +207
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
此外,我应该提到,如果我直接在 SQL 中 运行 DeleteUser 存储过程,所有内容都会按预期从两个 table 中删除,并且不会返回任何错误。
我在另一个论坛上遇到了 post,有人报告说设置 AllowPaging="True" 解决了这个问题。我不确定为什么这很重要,尽管我继续做了同样的事情并且我的问题也得到了解决。我很想知道为什么这解决了这个问题,但至少它现在可以工作了。