I've got a GridView that's sorted by a stored procedure with
ROW_NUMBER:
PROCEDURE dbo.GetCalendar sByStatusIDPage d
(
@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_sta tusID =
Calendars.fk_st atusID) as status,
ROW_NUMBER() OVER (ORDER BY received) AS RowRank
FROM Calendars
) AS CalendarsWithRo wNumbers
WHERE RowRank @startRowIndex AND RowRank <= (@startRowIndex +
@maximumRows) AND fk_statusID = @statusID
Here's the GridView:
<asp:GridView ID="GridView1" runat="server"
DataSourceID="C alendarsDataSou rce" AutoGenerateCol umns="False"
DataKeyNames="p k_calendarID" AllowPaging="Tr ue" PageSize="100">
<Columns>
<asp:BoundFie ld DataField="barc ode"
HeaderText="Bar code" SortExpression= "barcode" />
<asp:BoundFie ld DataField="rece ived"
HeaderText="Rec eived" SortExpression= "received" />
<asp:BoundFie ld DataField="atpr ess"
HeaderText="At Press" SortExpression= "atpress" />
<asp:TemplateFi eld>
<ItemTemplate >
<asp:CheckBox
ID="SelectCalen darCheckbox" runat="server" />
</ItemTemplate>
<HeaderTemplate >
<input id="chkAll"
onclick="javasc ript:SelectAllC heckboxes(this) ;" runat="server"
type="checkbox" />
</HeaderTemplate>
</asp:TemplateFie ld>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnToHandFi nishing" runat="server"
Text="Move To 'At Hand Finishing' Status"
OnClick="btnToH andFinishing_Cl ick" /><br />
<asp:ObjectData Source ID="CalendarsDa taSource"
runat="server" SelectMethod="G etCalendarsBySt atusIDPaged"
TypeName="Calen darsBLL" EnablePaging="T rue"
SelectCountMeth od="TotalNumber OfCalendarsBySt atusID">
<SelectParamete rs>
<asp:Paramete r DefaultValue="2 "
Name="statusID" Type="byte" />
</SelectParameter s>
</asp:ObjectDataS ource>
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 btnToHandFinish ing_Click(objec t sender, EventArgs e)
{
// Copy the items in the Rows collection into an array.
GridView gv1 = (GridView)Login View1.FindContr ol("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("Se lectCalendarChe ckbox")).Checke d;
if (result)
{
byte fk_statusID = 3;
DateTime athandfinishing = DateTime.Now;
int pk_calendarID =
(int)gv1.DataKe ys[row.RowIndex].Value;
Label1.Text += gv1.DataKeys[row.RowIndex].Value + " " +
row.Cells[1].Text + "<br />";
// Get filename
string filename =
methods.GetCale ndarFilename(pk _calendarID);
methods.UpdateC alendarAtHandFi nishing(fk_stat usID,
athandfinishing , pk_calendarID);
// Move Files to "READY_TO_PRINT " folder
string from = @"C:\HTTPPush\R EADY_TO_PRINT\" +
filename;
string to = @"C:\HTTPPush\P ROCESSED\" + filename;
// File.Move(from, to);
}
gv1.DataBind();
}
}
If I run it without the 'int pk_calendarID =
(int)gv1.DataKe ys[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.Argument OutOfRangeExcep tion: 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.DataKe ys[row.RowIndex].Value;
Line 38: Label1.Text +=
gv1.DataKeys[row.RowIndex].Value + " " + row.Cells[1].Text + "<br />";
Line 39: // key = GridView1.DataK eys[row.RowIndex];
Source File: c:\...\AtPress. aspx.cs Line: 37
Stack Trace:
[ArgumentOutOfRa ngeException: Index was out of range. Must be
non-negative and less than the size of the collection.
Parameter name: index]
System.Collecti ons.ArrayList.g et_Item(Int32 index) +2776189
System.Web.UI.W ebControls.Data KeyArray.get_It em(Int32 index) +9
AtPress.btnToHa ndFinishing_Cli ck(Object sender, EventArgs e) in
c:\...\AtPress. aspx.cs:37
System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e) +75
System.Web.UI.W ebControls.Butt on.RaisePostBac kEvent(String
eventArgument) +97
System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
eventArgument) +7
System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
+33
System.Web.UI.P age.ProcessRequ estMain(Boolean
includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint)
+4921
Any ideas?
Thanks in advance.
-Sean