GridView - Index was out of range.

S

ssims

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top