2014年7月13日 星期日

[RESOLVED] Object reference not set to an instance of an object


Hi,

In the calling project, I created the session variable like


Session.Remove("ite_nm");
Session["ite_nm"] = tb_itenm.Text;



while within the called project, due to that I refer to the session variable like


        protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lb_msg.Text = Session["ite_nm"].ToString();
...




I get this. why?

Server Error in '/App15' Application.

--------------------------------------------------------------------------------





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:





 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  



Stack Trace:







[NullReferenceException: Object reference not set to an instance of an object.]

   App15._Default.Page_Load(Object sender, EventArgs e) +82

   System.Web.UI.Control.LoadRecursive() +70

   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3177



 





--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18044 



after doing this


Session.Remove("ite_nm");

if you want to assign value for the same key, try following:


Session.Add("ite_nm", tb_itenm.Text);

Hope this helps!







Use below code


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(string.isNullorEmpty(Convert.ToString(Session["ite_nm"])))
{
lb_msg.Text = Session["ite_nm"].ToString();
...
}

I hope this will resolve your issue.





Hi;

tb_itenm.Text; 

Can be null ?


Thanks


Hi,


i think changing the line 


Session["ite_nm"].ToString();

to

convert.Tostring(Session["ite_nm"]);

should solve your problem because .Tostring does not handle null or empty 




shiningstar



after doing this
Session.Remove("ite_nm");

if you want to assign value for the same key, try following:


Session.Add("ite_nm", tb_itenm.Text);

Hope this helps!



I still get the same, using these


        protected void pass_click(object sender, EventArgs e)
{
Session.Remove("ite_nm");
//Session["ite_nm"] = tb_itenm.Text;
//Session.Add("ite_nm", tb_itenm.Text);
Session.Add("ite_nm", "abc");
...








are there any chances that you are clearing your session (maybe unknowingly) before fetching this value? Can you check the value of session object in watch window while debugging?



To check session exist or not use if(Session["ite_nm"]!=null) {...}

Thanks all.


Oned,


The point is, why I don't get the value of that session variable, while I already assigned one value to that, in the calling project, like


Session.Remove("ite_nm");
//Session["ite_nm"] = tb_itenm.Text;
//Session.Add("ite_nm", tb_itenm.Text);
Session.Add("ite_nm", "abc");
...







hello wmec,


    As you have said "In the calling project, I created the session variable like" and "while within the called project, due to that I refer to the session variable like" , can you please describe what do you mean by Project,
is calling and called project are two Separate project/web-sites or what. if these are two differet project or sites then you cannot read session of one project in the other project. as session are created per web-site, per user bases. two different
sites/project can never share sessions.



Yes, "2 project" means 2 separate applications, which have been deployed within the same server/site.



hmm, then two separate applications cannot share sessions , in fact each application has its own exclusive session . try to adopt some other techniques to sharing data.


沒有留言:

張貼留言