Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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;

}

  • Moderators
Posted

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

Visit...Bassic Software
Posted (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 by Robby

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...