Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to take the values form checkout.aspx page to confirm.aspx page.. using sessions.

but i am getting error

C#
Object Reference not set to Instance of an object Exception


on checkout.aspx page. i wrote the code as

C#
// Checkout.aspx

Order o = new Order();
        o.CustomerID = User.Identity.Name;
        Session["CustomerID"] = o.CustomerID;
        o.OrderDate = DateTime.Now;
        o.FirstName = firstnametxt.Text;
        Session["FirstName"] = o.FirstName;
        o.LastName = lastnametxt.Text;
        Session["LastName"] = o.LastName;
        o.Company = companytxt.Text;
        Session["Company"] = o.Company;
        o.Address = addresstxt.Text;
        Session["Address"] = o.Address;
        o.Country = dpcountry.Text;
        Session["Country"] = o.Country;
        o.City = citytxt.Text;
        Session["City"] = o.City;
        o.Province = provincetxt.Text;
        Session["Province"] = o.Province;
        o.ZipCode = zipcodetxt.Text;
        Session["ZipCode"] = o.ZipCode;
        o.Telephone = telephonetxt.Text;
        Session["Telephone"] = o.Telephone;
        o.Fax = faxtxt.Text;
        Session["Fax"] = o.Fax;


On Confirm.aspx page load i wrote the code. but not able to get value of session in label fieldss

C#
protected void Page_Load(object sender, EventArgs e)
    {
 lblfirstname.Text = Session["FirstName"].ToString();
        
        lbllastname.Text = Session["LastName"].ToString();
        lbladdress.Text = Session["Address"].ToString();
        lblphoneno.Text = Session["Telephone"].ToString();
        lblprovince.Text = Session["Province"].ToString();
        lblcity.Text = Session["City"].ToString();
        lblpostalcode.Text = Session["ZipCode"].ToString();
        bind();

    }
Posted

one of the possibilities is may be the code within Page_Load () of Confirm.aspx is getting executed before the piece of code in checkout.aspx. ensure that checkout.aspx event gets fired and code within the event gets executed before Conform.aspx's Page_Load().

Moreover, it will be good practice to initialize all the required properties of order object and set the session object at once.

Hope this will help you.
 
Share this answer
 
v2
Comments
RaviRanjanKr 16-Oct-11 16:24pm    
[Edited]just did Minor Formating[/Edited]
Debug the code and see what you are getting in session variables in you checkout.aspx.

You are getting this error just because session variables doesn’t contain a reference to any object in memory. If you try to call .ToString() on an object, and your variable is null, you’ll get this exception.

hope it helps :)
 
Share this answer
 
Comments
codegeekalpha 16-Oct-11 14:42pm    
now what i have to do??
Uday P.Singh 16-Oct-11 15:44pm    
did you debug the code?
Uday P.Singh 16-Oct-11 15:43pm    
please tell the reason for downvote...
RaviRanjanKr 16-Oct-11 16:18pm    
My 5+
Uday P.Singh 17-Oct-11 1:38am    
thanks Ravi :)
You need to configure session state via the web config file as shown here[^].
 
Share this answer
 
check the session variable names in both of the pages.
and debug it once line by line you will get exact location of the error try it
 
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