从GridView删除时出错

时间:2015-05-19 01:36:07

标签: c# sql asp.net gridview

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

存储过程:DeleteUser

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存储过程,则所有内容都会按预期从两个表中删除,并且不会返回任何错误。

1 个答案:

答案 0 :(得分:0)

我在另一个论坛上发现了一个帖子,其中一个人报告设置了AllowPaging =&#34; True&#34;解决了这个问题。我不确定为什么这应该重要,虽然我继续前进并做了同样的事情,我的问题也得到了解决。我有兴趣了解解决问题的原因,但至少它现在正在运作。