c# - Session sets the value successfully but gets null in MVC 5 -


i developing mvc 5 application , have specific controller session variables in application has 6 sessions, , working fine. wanted use session, have declared in session controller follows:

public int tempresult {      { return convert.toint32(session["tempresult"]); }      set { session["tempresult"] = value; } } 

now have controller inherits session controller , setting session variable tempresult in method as:

[httppost] public jsonresult coacodelength(mst m) {             var s = (from sets in db.abcs sets.name == "name" && sets.pre.tostring() == m.house select sets.id).single().tostring();      tempresult = convert.toint32(s); } 

now controller b inherits session controller calling method of controller , in method trying access session value of tempresult as:

[httppost] public actionresult new([bind(include = "id,name,house,number")] mst tr) {         tr.name = r.ccode(tr); // r instance of controller     db.msts.add(tr);     db.savechanges();     return json(tr); } 

and ccode method in controller as:

public string ccode (mst tr) {     int q = tempresult;     tr.name = q.tostring() + tr.house;     return tr.name;  } 

so, when ccode method in controller tries value of tempresult returns null object reference error.

what possible problem?

you cannot convert null int 32, meant if tempresult can null, should use this:

public int? tempresult {      { return convert.toint32(session["tempresult"]); }      set { session["tempresult"] = value; } } 

which allow integer variable accept null.

int? meaning integer variable can nullable.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -