Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Session's value in a class object?

I have assigned session a custom class's object like:

Services Obj = getServiceDetails(3); //getting all data of Service where Id = 3
Session["ServiceObj"] = Obj;

Now I want to assign its value to same class's another object in a model class. I don't know how to do it.

I tried but it is not valid way.:

Services oldObj = <Services>Session["ServiceObj"];

Pls Help me. I don't know how to do it.

like image 683
Dhwani Avatar asked May 29 '13 06:05

Dhwani


People also ask

Can I get session value in JavaScript?

Session is a variable on the backend server side, while JS is a previous script. There is no ready-made method in JS to get the value of Session, and it needs to be obtained through the server language. For example, java can be used to get the value of Session and assign it to JS variable.

How values are stored in session?

The Session Storage basically consists of 4 main methods. setItem(key, value): This method is used to set the value into the Session Storage based on the key. getItem(key): This method is used to get the value that is stored into the Session Storage. It takes a key and returns the value.


4 Answers

Correct type cast requires round brackets:

Services oldObj = (Services)Session["ServiceObj"];
like image 181
Igor Avatar answered Oct 18 '22 12:10

Igor


you should use Services oldObj = (Services)Session["ServiceObj"];

instead of Services oldObj = <Services>Session["ServiceObj"];

like image 32
Taj Avatar answered Oct 18 '22 12:10

Taj


not <Services> use (Services) for casting
like image 1
Kamil A. Avatar answered Oct 18 '22 12:10

Kamil A.


Cast it with your Class like this

Services obj = (Services)Session["ServiceObj"];
like image 1
Ali Shahbaz Avatar answered Oct 18 '22 12:10

Ali Shahbaz