简体   繁体   中英

Error deleting from GridView

In my GridView I have a list of employees. When I click the delete link I'm calling a stored procedure that deletes information from two tables. The first table works fine regardless. The second table works fine as well, however, if there were originally more than 4 rows of data corresponding with the given EmployeeID it returns an error even though all data is deleted as expected. If I remove the second delete statement and leave the Cities table as-is, I receive no errors so it must have something to do with that command. As the data from the second table does not appear in the GridView I'm not sure why this is happening. Any thoughts?

Stored Procedure: 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>

Error

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

Also, I should mention that if I run the DeleteUser stored procedure directly in SQL, everything deletes from both tables as expected with no errors returned.

I came across a post on a different forum where a person reported setting AllowPaging="True" resolved the issue. I'm not sure why that should matter, though I went ahead and did the same and my issue is also resolved. I'd be interested to understand why that fixed the issue, but at least its working now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM