Session value and pagable datagrid

shahab

Junior Contributor
Joined
Aug 14, 2003
Messages
206
Location
Iran(Middle East)
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?
 
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;
}
 
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
 
Oh I'm sorry but now I have another problem with this code:
New Code:
C#:
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:

C#:
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:
 
Last edited by a moderator:
Back
Top