Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > GridView - Index was out of range.

Reply
Thread Tools

GridView - Index was out of range.

 
 
ssims
Guest
Posts: n/a
 
      09-05-2006
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>
<asparameter 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
index.htm or index.html ? Robert Cooze NZ Computing 15 12-13-2005 05:53 PM
problem with index.html .(page is automatically gettin redirected to index.html) karthikeyavenkat Java 2 03-17-2005 10:01 PM
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index Stefan ASP .Net Datagrid Control 2 07-31-2004 05:52 AM



Advertisments