开发者

multiple instances of C# DataTable stored in session ..how this reference works?

开发者 https://www.devze.com 2023-03-27 06:51 出处:网络
I have a problem with DataTable stored in Session and code execution when retrieving it from this Session. Maybe this isn\'t a problem and is just me wrongly understanding how instances and references

I have a problem with DataTable stored in Session and code execution when retrieving it from this Session. Maybe this isn't a problem and is just me wrongly understanding how instances and references really work.

I have a POCO class User where I store all the data of logged in user. The POCO Class look like this:

public class User 
{
   public int UserID { get; set; }
   public string UserFirstName { get; set; }
   public string UserLastName { get; set; }
   public DataTable UserRights { get; set; }
}

Once the user is logged in (on login page) I create an instance of User class and I store all retrieved data from login into Session["User"]:

User _user = new User();
_user = MyLogin.GetUser() // method returns filled User object with data from db
Session["User"] = _user; // I store it into session

Then on my pages when I need data from Session for user rights (stored in datatable) I look into session and retrieve DataTable:

User _currUser = (User)Session["User"];
DataTable dt = new DataTable();
dt = _currUser.UserRights;

This code works OK, but some strange behavior starts when I look into Session for DataTable UserRights multiple times on the same page.

Example: I have two user controls on page and in each one of them I look into session for DataTable like in the code above. Then if I change DataTable object instance in one of the user controls code like this for example:

dt.Columns.Remove("RoleID");

...this change impacts the instance in second user control too(??). And the same goes if give instances different names:

// FirstUserControl.ascx.cs
User _currUser1 = (User)Session["User"];
DataTable dt1 = new DataTable();
dt1 = _currUser1.UserRights;
dt1.Columns.Remove("RoleID")

// SecondUserControl.ascx.cs
User _currUser2 = (User)Session["User"];
DataTable dt2 = new DataTable();
dt2 = _currUser2.UserRights; <--- dt2 is without column "RoleID" too!? 

Can som开发者_开发知识库ebody in a clear way explain to me why this is happening and how instances and references works in this example (it looks I don't understand them correctly). I always thought that if I execute code like the one above I would get dt1 and dt2 DataTable object which will not have anything in common when processing them.


DataTable is a reference type, dt1 and dt2 are just memory addresses to the chunk of memory holding the datatable, they always refer to the same object.

In this case you can use DataTable.Copy()

User _currUser1 = (User)Session["User"];
DataTable dt1 = _currUser1.UserRights.Copy();
dt1.Columns.Remove("RoleID")

// SecondUserControl.ascx.cs
User _currUser2 = (User)Session["User"];
DataTable dt2 = _currUser2.UserRights.Copy();
0

精彩评论

暂无评论...
验证码 换一张
取 消