PDA

Click to See Complete Forum and Search --> : Are objects (datasets) stored in session variables stored by reference?


arzoo
Apr 11th, 2005, 10:47 AM
I’m using a session variable to store a small dataset (bound to a datagrid). This is real basic stuff. When I modify the dataset in code, it seems that the session variable for the dataset is also modified, as if it’s stored byref. For example:
session(“dsStuff”) = dsStuff
dsStuff.Tables(0).Rows.Remove
Let’s say that the table contains 10 rows before the Remove statement. If I query the row count of the dataset object and the session variable, both will indicate 10 rows:

? dsStuff.Tables(0).Rows.Count
10
? ctype(session(“dsStuff”), Dataset).Tables(0).Rows.Count
10

After the remove statement I’ll query both again:

? dsStuff.Tables(0).Rows.Count
9
? ctype(session(“dsStuff”), Dataset).Tables(0).Rows.Count
9

I would have expected the session variable to still have 10 rows because I never reassigned it to the modified dataset. Here’s what the MS Help file says:

Values in session are typed as Object. If you set Option Strict On, you must cast the dataset to the appropriate type, as shown in the example.
You must update the cache any time there is a change to the dataset. For example, if you delete a record from the dataset, update the cache:

DsNorthwind1.Customers(4).Delete()
SqlDataAdapter1.Update(DsNorthwind1)
Session("dataset") = DsNorthwind1

Any thoughts?