tags:

views:

806

answers:

5

I've searched and am now at a loss as to the cause of my problem.

Here's the short version of my issue.

I have a gridview, a checkbox to toggle paging of the gridview on/off and a sql datasource with select and delete queries. If paging is on, I can click delete for a row in the gridview and it runs and the page refreshes no problem. If I turn paging off for the gridview, the delete query appears to run when I do a direct query against the table in sql server but the page fails to reload. I get the following 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) +280
System.Web.UI.WebControls.GridView.HandleDeleteCallback(Int32 affectedRows, Exception ex) +352
System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +119
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +938
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1152
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +190
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +172
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4920

Here is the page code:

<asp:SqlDataSource ID="dsSQL_ClaimsDetail" runat="server" ConnectionString="<%$ ConnectionStrings:activeDB_Connection %>"
     DeleteCommand="DELETE FROM [tblReturnClaimDetail] WHERE [ID] = @ID" ProviderName="<%$ ConnectionStrings:activeDB_Connection.ProviderName %>"
     SelectCommand="SELECT ID, HeaderID, CustomerSKUData, NikeInvoiceOrSalesOrderNumber, StyleandColorCode, StyleCode, ColorCode, StyleName, WholesaleDolrs, OriginalWholesaleDolrsPerUnit, Units, AccountDiscDolrsPerUnit, TotalNetPriceDolrs, HandlingandProcessing, Negotiated, NegotiatedReason, NegotiatedOptionType, NegotiatedOptionValue, FinalUnitPrice, FinalDolrs, FinalStandardDolrs, UpdateTime, UpdateUser, BatchUser, Approved, Denied, Pending, ClaimDisposition, FinalDecisionDate, ClaimRecordNumber, ASMName, Category, Gender, Other1, Other2, Other3, GBURegion, Division, BudgetManagerName, ClaimsBudgetYear, ClaimsBudgetQuarter, ApprovedDate, ReconciledBy, ReconciledDate, ReconcileOrigionalLineID FROM tblReturnClaimDetail WHERE (HeaderID = @claimID)">
     <DeleteParameters>
      <asp:Parameter Name="ID" />
     </DeleteParameters>
 <SelectParameters>
      <asp:ControlParameter ControlID="lbl_ClaimNumber" Name="claimID" PropertyName="Text" />
     </SelectParameters>
    </asp:SqlDataSource>
    <asp:CheckBox ID="chkbox_detailPagerToggle" runat="server" Text="View all styles at once" Visible="True" AutoPostBack="True" Checked="True" />&nbsp;</div>
    <asp:GridView ID="grid_Details" runat="server" AutoGenerateColumns="False"
     DataKeyNames="ID" DataSourceID="dsSQL_ClaimsDetail">
     <Columns>
      <asp:CommandField SelectText="Edit" ShowSelectButton="True">
       <HeaderStyle BackColor="Transparent" />
      </asp:CommandField>
      <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
       SortExpression="ID" />
      <asp:BoundField DataField="NikeInvoiceOrSalesOrderNumber" HeaderText="Nike Invoice /&lt;br /&gt;Sales Order #"
       HtmlEncode="False" SortExpression="NikeInvoiceOrSalesOrderNumber" />
      <asp:BoundField DataField="StyleCode" HeaderText="Style Code" SortExpression="StyleCode" />
      <asp:BoundField DataField="ColorCode" HeaderText="Color Code" SortExpression="ColorCode" />
      <asp:BoundField DataField="StyleName" HeaderText="Style Name" SortExpression="StyleName" />
      <asp:BoundField DataField="GBURegion" HeaderText="Channel" SortExpression="GBURegion" />
      <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />
      <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
      <asp:BoundField DataField="OriginalWholesaleDolrsPerUnit" DataFormatString="{0:c2}"
       HeaderText="WholeSale&lt;br /&gt;$/Unit" HtmlEncode="False" SortExpression="OriginalWholesaleDolrsPerUnit" />
      <asp:BoundField DataField="Units" HeaderText="Units" SortExpression="Units" />
      <asp:BoundField DataField="AccountDiscDolrsPerUnit" DataFormatString="{0:c2}" HeaderText="Discount&lt;br /&gt;$/Unit"
       HtmlEncode="False" SortExpression="AccountDiscDolrsPerUnit" />
      <asp:BoundField DataField="HandlingandProcessing" DataFormatString="{0:c2}" HeaderText="Handling &amp;&lt;br /&gt;Processing"
       HtmlEncode="False" SortExpression="HandlingandProcessing" />
      <asp:CheckBoxField DataField="Negotiated" HeaderText="Negotiated" SortExpression="Negotiated" />
      <asp:BoundField DataField="NegotiatedReason" HeaderText="Negotiated Reason" SortExpression="NegotiatedReason" />
      <asp:BoundField DataField="NegotiatedOptionType" HeaderText="Negotiated Type" SortExpression="NegotiatedOptionType" />
      <asp:BoundField DataField="NegotiatedOptionValue" HeaderText="Negotiated Value" SortExpression="NegotiatedOptionValue" />
      <asp:BoundField DataField="FinalDolrs" DataFormatString="{0:c2}" HeaderText="Final $ for Product"
       HtmlEncode="False" SortExpression="FinalDolrs">
       <ItemStyle Font-Bold="True" />
      </asp:BoundField>
      <asp:CommandField ShowDeleteButton="True">
       <HeaderStyle BackColor="Transparent" />
      </asp:CommandField>
     </Columns>
    </asp:GridView>

The only code behind for the 3 objects in question is on the checkbox:

Protected Sub chkbox_detailPagerToggle_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkbox_detailPagerToggle.CheckedChanged
    If Me.chkbox_detailPagerToggle.Checked = True Then
        Me.grid_Details.AllowPaging = False
    Else
        Me.grid_Details.AllowPaging = True
    End If
End Sub

Any ideas to get me looking in the right direction would be wonderful because I am at a loss for where to go next to solve this one.

A: 

It looks as if the GridView is confused about the number of rows that are getting deleted, and tries to set the selected index to an invalid value.

Does it only occur when the last row is deleted?

Are there any triggers firing when you do the delete? That can cause the @@RowCount to get messed up. Try adding "SET NOCOUNT OFF" before and "SET NOCOUNT ON" after your delete call.

I am guessing, but perhaps with paging on, the SetSelectedIndex code works slightly differently.

Robert Wagner
I didn't even think of checking the trigger. I just checked and nocount fixed it, thanks!
Have you thought about using SProcs instead of straight SQL? It'd get pretty messy with those NO COUNTS now.
Robert Wagner
I have considered it and am working on transitioning over to stored procedures but I inherited this project and there are currently far bigger issues to address than that at the moment, one being trying to begin normalizing a database made up of flat tables.
A: 

BRILLIANT! I was having this exact same issue. As soon as I read your post, bells went off. My Delete stored procedure does all kinds of stuff to other tables when a record is deleted. I added SET NOCOUNT ON in my stored procedure and now it works perfectly!

Thank you for you genius! :o)

A: 

This SET NOCOUNT OFF on the delete proc worked for me too! My GridView was deleting only when I turned off the delete trigger. I was really stuck! Thank you very much Robert!!!

BetsyD
A: 

I had a stored procedure that deleting data from three tables (two junction tables and one regular table). I had the same issue, where the data would delete but I would still get the same error. I solved this using above suggestions. Before the DELETE commands in the stored procedure I put "SET NOCOUNT ON" and this seemed to solve the problem.

Allison Christiansen
A: 

Robert Wagner YOU ARE A GENIUS ...... Thanks a LOT xoxoxox

I din touch my trigger. in my aspx file i just added SET NOCOUNT ON thats it..

DeleteCommand="SET NOCOUNT ON;DELETE FROM blah blah blah...."

acyahoo