c# webforms vs 2003

i have this data grid call dgresults which i m using the page index change event and item command event my problem is every time when i load the data grid and try to change the pages it's giving me an exception. that's because every time the page index fires item command automatically fires is there a way around this problem

code in red is where exception occurs but it only occurs if try to change the datagrid page

below are my two event s and the exception

item command event

private void dgresults_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e1)
			TableCell itemcell2=e1.Item.Cells[0];
					TableCell itemcell=e1.Item.Cells[1];
					selbooks[addcount]= itemcell.Text;



page index change event

private void dgresults_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)

			dgresults.CurrentPageIndex = e.NewPageIndex;

exception massage

Specified argument was out of the range of valid values. 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: Specified argument was out of the range of valid values. Parameter name: index

Source Error: 

Line 202:				if(addcount<100)
Line 203:				{
Line 204:					TableCell itemcell=e1.Item.Cells[1];
Line 205:						
Line 206:					selbooks[addcount]= itemcell.Text;

Source File: c:\inetpub\wwwroot\newtechbooks\books_loged.aspx.cs    Line: 204 

Stack Trace: 

[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
   System.Web.UI.ControlCollection.get_Item(Int32 index) +58
   System.Web.UI.WebControls.TableCellCollection.get_Item(Int32 index)
   NewTechBooks.Books_loged.dgresults_ItemCommand(Object source, DataGridCommandEventArgs e1) in c:\inetpub\wwwroot\newtechbooks\books_loged.aspx.cs:204
   System.Web.UI.WebControls.DataGrid.OnItemCommand(DataGridCommandEventArgs e)
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() +1277



You might need to test the CommandName property of the DataGridCommandEventArgs to only execute your command code when needed.
For an example see the ItemsGrid_Command function here MSDN DataGrid.ItemCommand Event

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.