Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi!I am getting the following error:
<<object>>
This picture bellow is the GridView for the first form that I have:

The first page is containing the grid view that has an hyper-link 'Buy'
The hyper-link will redirect you to another page which should display the specific information coming from the previous page
This the first page that have the GridView:
C#
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        
        int newIndex = e.NewSelectedIndex;//Row that the user is selecting(!e is a parameter)
        object EventID = GridView1.DataKeys[newIndex].Value;


        string Game = GridView1.Rows[newIndex].Cells[0].Text;
        string Location=GridView1.Rows[newIndex].Cells[1].Text;
        string Address = GridView1.Rows[newIndex].Cells[2].Text;
        string Date = GridView1.Rows[newIndex].Cells[3].Text;
        string Hour = GridView1.Rows[newIndex].Cells[4].Text;
        
        MessageLabel.Text = string.Format("You have select<br />Match:{0}<br /> [Event ID:{1}],", Game,EventID);
        Session["Game"] = MessageLabel.Text;
       
    }

This second page is loading the session for the first page,and it were the page in point the error('GameLabel.Text = Session["Game"].ToString();'):

C#
protected void Page_Load(object sender, EventArgs e)
    {
        GameLabel.Text = Session["Game"].ToString();
    } 

Help resolve this problem!
Posted
Updated 17-Mar-12 5:17am
v2
Comments
Sergey Alexandrovich Kryukov 26-Feb-12 20:20pm    
Resolve what? Please format code correctly, using "pre" tags, show the line where exception was thrown. Run it under debugger -- you will see the bug by yourself.
--SA

The problem is with the call to the ToString method here GameLabel.Text = Session["Game"].ToString();
The session variable is null. You are trying to call the ToString on something that does not exist.

You need to check if the session variable is null before you do anything else with it. Try this.

C#
if(Session["Game"] != null)
{
    GameLabel.Text = Session["Game"].ToString();
}
 
Share this answer
 
C#
ToString()
does not accept null values but
C#
Convert.ToString()
does
so You can Use Like this

GameLabel.Text = Convert.ToString(Session["Game"]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900