shahab Posted March 16, 2004 Posted March 16, 2004 Dear Friends, I am using a pagable datagrid and a session like this: Session["NewsId"]=DataGrid1.DataKeys[(int)e.Item.ItemIndex]; When I browsing through the pages of datagrid this error is shown to me: 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 179: { Line 180: //Label3.Text=DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); Line 181: Session["NewsId"]=DataGrid1.DataKeys[(int)e.Item.ItemIndex]; What should I do in order to solve this problem? Quote
kahlua001 Posted March 16, 2004 Posted March 16, 2004 Can you post more code. Perhaps the entire sub. Quote
Moderators Robby Posted March 16, 2004 Moderators Posted March 16, 2004 shahab, does this occur soon after you alter the record count of the datatable. Quote Visit...Bassic Software
shahab Posted March 17, 2004 Author Posted March 17, 2004 private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { //Label3.Text=DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); Session["NewsId"]=DataGrid1.DataKeys[(int)e.Item.ItemIndex]; /**/ if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "edit") { Response.Redirect("EditNews.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "first") { SetInDefaultWebForm(); Response.Redirect("../Default.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "del") { DelNews(); //Response.Redirect("DelNews.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "DelF") { RemoveFromDefaultWebForm(); Response.Redirect("../Default.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "َAdd") { Response.Redirect("AddNews.aspx"); } } //********************************************************** private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex=e.NewPageIndex; } Quote
shahab Posted March 17, 2004 Author Posted March 17, 2004 This occured when I make my DataGrid Pagable! At the First it wasn't! Quote
Moderators Robby Posted March 17, 2004 Moderators Posted March 17, 2004 Check the value of e.NewPageIndex before setting it to the current page Quote Visit...Bassic Software
Moderators Robby Posted March 17, 2004 Moderators Posted March 17, 2004 e.NewPageIndex is read-only, I ment verify its' value prior to assigning it to the CurrenPageIndex of the Datagrid Quote Visit...Bassic Software
shahab Posted March 17, 2004 Author Posted March 17, 2004 1-In page load u mean?Where? 2-How can I verify it?Code Please :) Quote
Moderators Robby Posted March 17, 2004 Moderators Posted March 17, 2004 You can do it in the PageIndexChanged event or anywhere else that you assign a value to the CurrentPageIndex property. Compare the DataGrid1.PageCount to the e.NewPageIndex and don't allow less than 0 Quote Visit...Bassic Software
shahab Posted March 20, 2004 Author Posted March 20, 2004 (edited) :rolleyes: Edited March 20, 2004 by shahab Quote
Administrators PlausiblyDamp Posted March 20, 2004 Administrators Posted March 20, 2004 shouldn't (DataGrid1.PageCount - e.NewPageIndex) <= 0 be (DataGrid1.PageCount - e.NewPageIndex) > 0 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
shahab Posted March 20, 2004 Author Posted March 20, 2004 (edited) Oh I'm sorry but now I have another problem with this code: New Code: private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { //DataGrid1.DataKeys[(int)e.Item.ItemIndex]=-1; //Label3.Text=DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); //Session["NewsId"]=DataGrid1.DataKeys[(int)e.Item.ItemIndex]; //DataGrid1.SelectedItem.ItemIndex.ToString();Wrong if ((DataGrid1.PageCount - e.NewPageIndex) > 0) { DataGrid1.CurrentPageIndex=e.NewPageIndex; //Session["NewsId"]=DataGrid1.DataKeys[0]; bindGrid1(); } /**/ if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "edit") { Response.Redirect("EditNews.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "first") { SetInDefaultWebForm(); Response.Redirect("../Default.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "del") { DelNews(); //Response.Redirect("DelNews.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "DelF") { RemoveFromDefaultWebForm(); Response.Redirect("../Default.aspx"); } if (((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue == "َAdd") { Response.Redirect("AddNews.aspx"); } } private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { try { if ((DataGrid1.PageCount - e.NewPageIndex) > 0) { DataGrid1.CurrentPageIndex=e.NewPageIndex; //Session["NewsId"]=DataGrid1.DataKeys[0]; bindGrid1(); } } catch (Exception e01) { Label3.Text = e01.Message.ToString(); } } } Error message : Object reference not set to an instance of an object. 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.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 169: //Session["NewsId"]=DataGrid1.DataKeys[(int)e.Item.ItemIndex]; Line 170: //DataGrid1.SelectedItem.ItemIndex.ToString();Wrong Line 171: if ((DataGrid1.PageCount - e.NewPageIndex) > 0) Line 172: { Line 173: DataGrid1.CurrentPageIndex=e.NewPageIndex; D:\Inetpub\wwwroot\School\AdminNews\ChooseNews.aspx.cs(171): 'System.Web.UI.WebControls.DataGridCommandEventArgs' does not contain a definition for 'NewPageIndex' :confused: :confused: :confused: Edited March 20, 2004 by Robby Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.