ntshah
ntshah

Reputation: 31

ASP.Net Session variable stores the actual value or mere reference?

I have following code, where I am fetcing a datatable stored in Session in a local variable, and updating the datatable. Next time, when I access the session, the updated values are fetched. (in my case, first row deleted datatable fetched for variable objTable1):

 protected void Page_Load(object sender, EventArgs e)
    {
        DataTable objTable = (DataTable)Session["dTable"];

        objTable.Rows[0].Delete();

        DataTable objTable1 = (DataTable)Session["dTable"];

    }

I thought, local variables make a copy of the session variable and updation in that, do not update the session variable.

Regards.

Upvotes: 3

Views: 825

Answers (1)

Glauco Vinicius
Glauco Vinicius

Reputation: 2647

You're dealing with a reference to an object in the session so this is how it should work.

If you want to work with a different object, you should make a clone of the object in the session to work with, then replace the one stored in the session with the clone if you want to save any changes.

Upvotes: 1

Related Questions