GridView - Index was out of range.

Discussion in 'ASP .Net' started by ssims, Sep 5, 2006.

  1. ssims

    ssims Guest

    I've got a GridView that's sorted by a stored procedure with
    ROW_NUMBER:

    PROCEDURE dbo.GetCalendarsByStatusIDPaged
    (
    @startRowIndex int,
    @maximumRows int,
    @statusID int
    )
    AS
    SELECT pk_calendarID, fk_statusID, filename, barcode, received,
    atpress, athandfinishing, shipped, reprint, status
    FROM
    (
    SELECT pk_calendarID, fk_statusID, filename, barcode,
    received, atpress, athandfinishing, shipped, reprint,
    (SELECT status FROM Statuses WHERE Statuses.pk_statusID =
    Calendars.fk_statusID) as status,
    ROW_NUMBER() OVER (ORDER BY received) AS RowRank
    FROM Calendars
    ) AS CalendarsWithRowNumbers
    WHERE RowRank > @startRowIndex AND RowRank <= (@startRowIndex +
    @maximumRows) AND fk_statusID = @statusID

    Here's the GridView:

    <asp:GridView ID="GridView1" runat="server"
    DataSourceID="CalendarsDataSource" AutoGenerateColumns="False"
    DataKeyNames="pk_calendarID" AllowPaging="True" PageSize="100">
    <Columns>
    <asp:BoundField DataField="barcode"
    HeaderText="Barcode" SortExpression="barcode" />
    <asp:BoundField DataField="received"
    HeaderText="Received" SortExpression="received" />
    <asp:BoundField DataField="atpress"
    HeaderText="At Press" SortExpression="atpress" />
    <asp:TemplateField>
    <ItemTemplate>
    <asp:CheckBox
    ID="SelectCalendarCheckbox" runat="server" />
    </ItemTemplate>
    <HeaderTemplate>
    <input id="chkAll"
    onclick="javascript:SelectAllCheckboxes(this);" runat="server"
    type="checkbox" />
    </HeaderTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    <br />
    <asp:Button ID="btnToHandFinishing" runat="server"
    Text="Move To 'At Hand Finishing' Status"
    OnClick="btnToHandFinishing_Click" /><br />
    <asp:ObjectDataSource ID="CalendarsDataSource"
    runat="server" SelectMethod="GetCalendarsByStatusIDPaged"
    TypeName="CalendarsBLL" EnablePaging="True"
    SelectCountMethod="TotalNumberOfCalendarsByStatusID">
    <SelectParameters>
    <asp:parameter DefaultValue="2"
    Name="statusID" Type="byte" />
    </SelectParameters>
    </asp:ObjectDataSource>

    Then, I've got a button to take selected rows (via checkboxes in a
    TemplateField) and do something with them in the database:

    protected void btnToHandFinishing_Click(object sender, EventArgs e)
    {
    // Copy the items in the Rows collection into an array.
    GridView gv1 = (GridView)LoginView1.FindControl("GridView1");
    GridViewRow[] rowArray = new GridViewRow[gv1.Rows.Count];
    gv1.Rows.CopyTo(rowArray, 0);

    // Iterate though the array and display the value in the first
    cell of the row.
    foreach (GridViewRow row in rowArray)
    {
    bool result =
    ((CheckBox)row.FindControl("SelectCalendarCheckbox")).Checked;
    if (result)
    {
    byte fk_statusID = 3;
    DateTime athandfinishing = DateTime.Now;
    int pk_calendarID =
    (int)gv1.DataKeys[row.RowIndex].Value;
    Label1.Text += gv1.DataKeys[row.RowIndex].Value + " " +
    row.Cells[1].Text + "<br />";
    // Get filename
    string filename =
    methods.GetCalendarFilename(pk_calendarID);
    methods.UpdateCalendarAtHandFinishing(fk_statusID,
    athandfinishing, pk_calendarID);
    // Move Files to "READY_TO_PRINT" folder
    string from = @"C:\HTTPPush\READY_TO_PRINT\" +
    filename;
    string to = @"C:\HTTPPush\PROCESSED\" + filename;
    // File.Move(from, to);
    }
    gv1.DataBind();
    }
    }

    If I run it without the 'int pk_calendarID =
    (int)gv1.DataKeys[row.RowIndex].Value;' line, it runs fine, and
    displays the appropriate values. But if I run it with the
    pk_calendarID line it fails with :

    Index was out of range. Must be non-negative and less than the size of
    the collection.
    Parameter name: index
    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: Index was out of
    range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Source Error:
    Line 35: byte fk_statusID = 3;
    Line 36: DateTime athandfinishing = DateTime.Now;
    Line 37: int pk_calendarID =
    (int)gv1.DataKeys[row.RowIndex].Value;
    Line 38: Label1.Text +=
    gv1.DataKeys[row.RowIndex].Value + " " + row.Cells[1].Text + "<br />";
    Line 39: // key = GridView1.DataKeys[row.RowIndex];

    Source File: c:\...\AtPress.aspx.cs Line: 37

    Stack Trace:

    [ArgumentOutOfRangeException: Index was out of range. Must be
    non-negative and less than the size of the collection.
    Parameter name: index]
    System.Collections.ArrayList.get_Item(Int32 index) +2776189
    System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index) +9
    AtPress.btnToHandFinishing_Click(Object sender, EventArgs e) in
    c:\...\AtPress.aspx.cs:37
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String
    eventArgument) +97

    System.Web.UI.WebControls.Button.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)
    +33
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    +4921

    Any ideas?

    Thanks in advance.
    -Sean
     
    ssims, Sep 5, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yyt

    Index was out of range?

    yyt, Dec 18, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    482
    Hans Kesting
    Dec 19, 2003
  2. Clark Choi
    Replies:
    1
    Views:
    4,524
    Alvin Bruney
    Jan 10, 2004
  3. DaveF

    Insertion index was out of range.

    DaveF, May 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,519
    Alvin Bruney [MVP]
    May 30, 2004
  4. hari
    Replies:
    3
    Views:
    12,548
    Vimal
    Nov 15, 2006
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    306
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page