Click here to Skip to main content
11,581,130 members (64,068 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 19-Feb-12 19:26pm
Comments
Varun Sareen at 20-Feb-12 1:33am
   
Please tell the process how you are going? and when on which event you face this error?
rockpune at 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 at 20-Feb-12 1:43am
   
Case sensitivity in Cookie name may be the issue also
emad_hawaree at 20-Feb-12 1:44am
   
What about the exceptions?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

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

Hope it helps Smile | :)
  Permalink  
Comments
rockpune at 20-Feb-12 1:42am
   
salamwalekum rahman bhai thanx...............


azhar
Mohammad A Rahman at 20-Feb-12 1:48am
   
Does it solve your problem ?
rockpune at 20-Feb-12 1:52am
   
ok
rockpune at 20-Feb-12 1:52am
   
i get it
Varun Sareen at 20-Feb-12 1:55am
   
accept this as your solution and rate it :)
Mohammad A Rahman at 20-Feb-12 1:56am
   
Thanks Varun :)
Varun Sareen at 20-Feb-12 2:06am
   
your welcome rahman
Mohammad A Rahman at 20-Feb-12 1:56am
   
Does renaming the MyCookie name solve your problem?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Session"Mysession"].ToString();
ViewState["MyViewState"].ToString();
USE
Convert.ToString( Session"Mysession"]);
Convert.ToString(ViewState["MyViewState"]);
 
  Permalink  
Comments
Member 8821727_GhostAnswer at 4-Jan-13 6:40am
   
'Session"Mysession"]' should be 'Session["Mysession"]' in your solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.

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

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


Hope this will help you in your code,

thanks
-Amit
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 210
1 Frankie-C 195
2 Sergey Alexandrovich Kryukov 85
3 F-ES Sitecore 80
4 Andy Lanng 75
0 OriginalGriff 1,108
1 Sergey Alexandrovich Kryukov 785
2 Abhinav S 622
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 20 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100