Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        Response.Cookies.Add(new HttpCookie("MyCookie", ASPxTextBox2.Text));
    }
    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        Session["Mysession"] = ASPxTextBox3.Text;
    }
    protected void ASPxButton3_Click(object sender, EventArgs e)
    {
        ViewState.Add("MyViewState",ASPxTextBox4.Text);
    }
    protected void ASPxButton4_Click(object sender, EventArgs e)
    {
        ASPxLabel1.Text = Server.HtmlEncode(Request.Cookies["Mycookie"].Value);
        ASPxLabel2.Text = Session["Mysession"].ToString();
        ASPxLabel3.Text = ViewState["MyViewState"].ToString();
    }
}



error...

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 29: ASPxLabel1.Text = Server.HtmlEncode(Request.Cookies["Mycookie"].Value);
Line 30: ASPxLabel2.Text = Session["Mysession"].ToString();
Line 31: ASPxLabel3.Text = ViewState["MyViewState"].ToString();
Line 32: }
Posted
Comments
Varun Sareen 20-Feb-12 1:33am    
Please tell the process how you are going? and when on which event you face this error?
rockpune 20-Feb-12 1:35am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
Response.Cookies.Add(new HttpCookie("MyCookie", ASPxTextBox2.Text));
}
protected void ASPxButton2_Click(object sender, EventArgs e)
{
Session["Mysession"] = ASPxTextBox3.Text;
}
protected void ASPxButton3_Click(object sender, EventArgs e)
{
ViewState.Add("MyViewState",ASPxTextBox4.Text);
}
protected void ASPxButton4_Click(object sender, EventArgs e)
{
ASPxLabel1.Text = Server.HtmlEncode(Request.Cookies["Mycookie"].Value);
ASPxLabel2.Text = Session["Mysession"].ToString();
ASPxLabel3.Text = ViewState["MyViewState"].ToString();
}
}
Varun Sareen 20-Feb-12 1:43am    
Case sensitivity in Cookie name may be the issue also
Emad Al Hawary 20-Feb-12 1:44am    
What about the exceptions?

Your are setting cookie value as MyCookie but retrieving as Mycookie. The name difference.

Setting Code:
C#
Response.Cookies.Add(new HttpCookie("MyCookie", ASPxTextBox2.Text));


Retirve Code:
C#
ASPxLabel1.Text = Server.HtmlEncode(Request.Cookies["Mycookie"].Value);


Hope it helps :)
 
Share this answer
 
Comments
rockpune 20-Feb-12 1:42am    
salamwalekum rahman bhai thanx...............


azhar
Mohammad A Rahman 20-Feb-12 1:48am    
Does it solve your problem ?
rockpune 20-Feb-12 1:52am    
ok
rockpune 20-Feb-12 1:52am    
i get it
Varun Sareen 20-Feb-12 1:55am    
accept this as your solution and rate it :)
Dear Friend,

As you haven't told in your problem that when you faced this error, I am giving you the solution for it on the basis of my presumption that you have clicked on the aspx button (ASPxButton4) first and as all the variable i.e., Session, View and Cookie are not yet initialized you are facing the problem :-


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.
comes. In order to solve it you first have to initialize all the variables and then in the last click on the aspx button. Or you can initialize all the variables in the Page_Load event also.

Please refer this link for more details:- http://support.microsoft.com/kb/810098[^]

Thanks
 
Share this answer
 
v2
C#
Session"Mysession"].ToString();
ViewState["MyViewState"].ToString();
USE
Convert.ToString( Session"Mysession"]);
Convert.ToString(ViewState["MyViewState"]);
 
Share this answer
 
Comments
Member 8821727_GhostAnswer 4-Jan-13 6:40am    
'Session"Mysession"]' should be 'Session["Mysession"]' in your solution.
try to remove
using System.Linq;
Sometimes it is working without using LINQ


Maybe you're trying to access a member of a null reference; i.e. one of the variables here is null. Without knowing the line number it's difficult to say which
 
Share this answer
 
v2
Hi,

Most of the time Object Reference exception is cause by invalid name or accessing field for null object.

So, when you do coding. make sure you have all the required checking conditions. don't bother about number of lines. sometime people think, reducing line of code is good. Correct. but also minimize possibility of errors.

example :

if you are accessing cookies value Request.Cookies["Mycookie"].Value please use condition before this call.

C#
if(Request.Cookies["Mycookie"] != null)
{
ASPxLabel1.Text = Server.HtmlEncode(Request.Cookies["Mycookie"].Value);
}


OR
C#
ASPxLabel1.Text = 
        Request.Cookies["Mycookie"] ? "" : Server.HtmlEncode(Request.Cookies["Mycookie"].Value);



Hope this will help you in your code,

thanks
-Amit
 
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