Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从GridView中删除时出错_C#_Sql_Asp.net_Gridview - Fatal编程技术网

C# 从GridView中删除时出错

C# 从GridView中删除时出错,c#,sql,asp.net,gridview,C#,Sql,Asp.net,Gridview,在我的GridView中,我有一个员工列表。当我单击delete链接时,我调用了一个存储过程,该过程从两个表中删除信息。不管怎样,第一个表都可以正常工作。第二个表也可以正常工作,但是,如果最初与给定EmployeeID对应的数据行数超过4行,则即使所有数据按预期删除,它仍会返回一个错误。如果删除第二条delete语句并保持Cities表不变,则不会收到任何错误,因此它一定与该命令有关。由于第二个表中的数据没有出现在GridView中,我不确定为什么会发生这种情况。有什么想法吗 存储过程:Dele

在我的GridView中,我有一个员工列表。当我单击delete链接时,我调用了一个存储过程,该过程从两个表中删除信息。不管怎样,第一个表都可以正常工作。第二个表也可以正常工作,但是,如果最初与给定EmployeeID对应的数据行数超过4行,则即使所有数据按预期删除,它仍会返回一个错误。如果删除第二条delete语句并保持Cities表不变,则不会收到任何错误,因此它一定与该命令有关。由于第二个表中的数据没有出现在GridView中,我不确定为什么会发生这种情况。有什么想法吗

存储过程:DeleteUser

DELETE FROM Users WHERE ID=@ID
DELETE FROM Cities WHERE ID=@ID
ASPX


另外,我应该提到,如果我直接在SQL中运行DeleteUser存储过程,所有内容都会按预期从两个表中删除,不会返回任何错误。

我在另一个论坛上看到一篇帖子,其中有人报告设置AllowPaging=“True”解决了这个问题。我不确定这有什么关系,尽管我也这么做了,我的问题也解决了。我很想了解为什么这解决了这个问题,但至少它现在起作用了

<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