【问题标题】:Error deleting from GridView从 GridView 删除时出错
【发布时间】:2015-05-19 01:36:06
【问题描述】:

在我的 GridView 中,我有一个员工列表。当我单击删除链接时,我正在调用从两个表中删除信息的存储过程。无论如何,第一张桌子都可以正常工作。第二个表也可以正常工作,但是,如果最初有超过 4 行与给定 EmployeeID 对应的数据,即使所有数据都按预期删除,它也会返回错误。如果我删除第二个删除语句并按原样保留 Cities 表,我不会收到任何错误,因此它一定与该命令有关。由于第二个表中的数据没有出现在 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 存储过程,则会按预期从两个表中删除所有内容,并且不会返回任何错误。

【问题讨论】:

    标签: c# sql asp.net gridview


    【解决方案1】:

    我在另一个论坛上看到一个帖子,有人报告说设置 AllowPaging="True" 解决了这个问题。我不确定为什么这很重要,尽管我继续做同样的事情并且我的问题也得到了解决。我很想知道为什么这解决了这个问题,但至少它现在可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      相关资源
      最近更新 更多