the error is:
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 96: protected void GVexisting_RowDeleting(object sender, GridViewDeleteEventArgs e)
Line 97: {
Line 98: int intId = (int)GVexisting.DataKeys[e.RowIndex].Value;
Line 99:
Line 100: string sql = "DELETE FROM Events WHERE ID = @id";
Source File: C:\Users\Dusitn\documents\visual studio 2010\Projects\Dustins website\Dustins website\calAdmin.aspx.cs Line: 98
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) +7487176
System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index) +12
Dustins_website.calAdmin.GVexisting_RowDeleting(Object sender, GridViewDeleteEventArgs e) in C:\Users\Dusitn\documents\visual studio 2010\Projects\Dustins website\Dustins website\calAdmin.aspx.cs:98
System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +133
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +569
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +869
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +207
System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Code for page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="calAdmin.aspx.cs" Inherits="Dustins_website.calAdmin" MasterPageFile="site.master" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<link href="Styles/calAdmin.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="wrapper">
<br />
<h1 class="center" style="color:black;">Date Book</h1>
<hr />
<div class="float_left" style="margin-right:15px;">
<asp:Calendar ID="Calendar1" runat="server" BackColor="White"
BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt"
ForeColor="Black" Height="249px" NextPrevFormat="FullMonth" Width="450px"
onselectionchanged="Calendar1_Selectionchanged"
onvisiblemonthchanged="Calendar1_VisibleMonthChanged">
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
<NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333"
VerticalAlign="Bottom" />
<OtherMonthDayStyle ForeColor="#999999" />
<SelectedDayStyle BackColor="White" ForeColor="#FF3300" BorderColor="Black"
BorderStyle="Dotted" />
<TitleStyle BackColor="White" BorderColor="#CCCCCC" BorderWidth="3px"
Font-Bold="True" Font-Size="12pt" ForeColor="#333399"
BorderStyle="Solid" />
<TodayDayStyle BackColor="#99CCFF" />
<WeekendDayStyle BackColor="#999999" ForeColor="White" />
</asp:Calendar>
</div>
<div id="formData">
<p>
<asp:Label ID="datelbl" runat="server" Text="Date: "></asp:Label>
<asp:TextBox ID="date" runat="server"></asp:TextBox></p>
<p>
<asp:Label ID="timelbl" runat="server" Text="Time: "></asp:Label>
<asp:TextBox ID="time" runat="server"></asp:TextBox></p>
<p>
<asp:Label ID="eventlbl" runat="server" Text="Event: "></asp:Label>
<asp:TextBox ID="NewEvent" runat="server"></asp:TextBox></p>
<p>
<asp:Label ID="detailslbl" runat="server" Text="Details: "></asp:Label></p>
<p>
<asp:TextBox ID="details" runat="server" Width="400px" Height="55px"></asp:TextBox>
</p>
<p style="text-align:center">
<asp:Button ID="submitbtn" runat="server" Text="Submit"
onclick="submitbtn_Click" />
<asp:Button ID="clearbtn" runat="server" Text="Clear"
onclick="clearbtn_Click" /></p>
</div>
<hr />
<div class="center">
<asp:GridView ID="GVexisting" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" DataSourceID="SqlCal" EnableModelValidation="True"
ForeColor="Black" GridLines="Vertical" HorizontalAlign="Center"
Width="890px" onrowupdating="GVexisting_RowUpdating" ShowFooter="True"
onrowdeleting="GVexisting_RowDeleting">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="_date" HeaderText="Date" dataformatstring="{0:MMMM d, yyyy}"
SortExpression="_date" />
<asp:BoundField DataField="_time" HeaderText="Time"
SortExpression="_time" DataFormatString="{0:t}" />
<asp:BoundField DataField="event" HeaderText="Event" SortExpression="event" />
<asp:BoundField DataField="details" HeaderText="Details"
SortExpression="details" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Blue" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlCal" runat="server"
ConnectionString="<%$ ConnectionStrings:calenderConnection %>"
SelectCommand="SELECT * FROM [Events]"
ProviderName="<%$ ConnectionStrings:calenderConnection.ProviderName %>">
</asp:SqlDataSource>
</div>
</div>
</asp:Content>
Back Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Dustins_website
{
public partial class calAdmin : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int month = DateTime.Now.Month;
updateMonth(month);
}
}
private void updateMonth(int month)
{
string sql = "SELECT * FROM Events WHERE MONTH(_date) = " + month.ToString();
SqlCal.SelectCommand = sql;
GVexisting.DataBind();
}
protected void Calendar1_Selectionchanged(object sender, EventArgs e)
{
DateTime TIME = DateTime.Now;
date.Text = Calendar1.SelectedDate.ToShortDateString();
time.Text = TIME.ToShortTimeString();
}
protected void clearbtn_Click(object sender, EventArgs e)
{
date.Text = "";
time.Text = "";
NewEvent.Text = "";
details.Text = "";
}
protected void submitbtn_Click(object sender, EventArgs e)
{
string sql = "INSERT INTO events(_date, _time, event, details)";
sql += "VALUES(@date, @time, @event, @details)";
SqlCal.InsertParameters.Add("date", date.Text.Trim());
SqlCal.InsertParameters.Add("time", time.Text.Trim());
SqlCal.InsertParameters.Add("event", NewEvent.Text.Trim());
SqlCal.InsertParameters.Add("details", details.Text.Trim());
SqlCal.InsertCommand = sql;
SqlCal.Insert();
date.Text = "";
time.Text = "";
NewEvent.Text = "";
details.Text = "";
}
protected void GVexisting_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//get primary key
int intId = (int)GVexisting.DataKeys[e.RowIndex].Value;
//get row
GridViewRow row = GVexisting.Rows[e.RowIndex];
TextBox txtdate = (TextBox)row.Cells[1].Controls[0];
TextBox txttime = (TextBox)row.Cells[2].Controls[0];
TextBox txtnewEvent = (TextBox)row.Cells[3].Controls[0];
TextBox txtdetails = (TextBox)row.Cells[4].Controls[0];
String sql = "UPDATE Events SET _date = @date, _time = @time, event = @newEvent,";
sql += "details = @details WHERE ID = @id";
SqlCal.UpdateParameters.Add("date", txtdate.Text.Trim());
SqlCal.UpdateParameters.Add("time", txttime.Text.Trim());
SqlCal.UpdateParameters.Add("newEvent", txtnewEvent.Text.Trim());
SqlCal.UpdateParameters.Add("details", txtdetails.Text.Trim());
SqlCal.UpdateCommand = sql;
SqlCal.Update();
}
protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
int month = DateTime.Now.Month;
updateMonth(month);
}
protected void GVexisting_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int intId = (int)GVexisting.DataKeys[e.RowIndex].Value;
string sql = "DELETE FROM Events WHERE ID = @id";
SqlCal.DeleteParameters.Add("id", intId.ToString());
SqlCal.DeleteCommand = sql;
SqlCal.Delete();
}
}
}
What is causing this error?

New Topic/Question
Reply


MultiQuote





|