By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,694 Members | 1,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,694 IT Pros & Developers. It's quick & easy.

GridView - Index was out of range.

P: n/a
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="TotalNumberOfCalendarsByStatusI D">
<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(In t32 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.RaisePostBackEven t(String
eventArgument) +97

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
+33
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+4921

Any ideas?

Thanks in advance.
-Sean

Sep 5 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.