Session Variables Strange Behaviour

G

Guest

Hi all,
I'm having a problem with session variables in vs .net 2003. I start by
doing this:
If Not IsPostBack Then
Dim dt As DataTable
Me.daAlias.Fill(Me.DsPhysicalAlias)
dt = Me.DsPhysicalAlias.Tables(0)
Session("tblAlias") = dt
Session("tblAliasOriginal") = dt
BindGrid()
End If
I want to load dt into two session variables, make whatever changes I want
to tblAlias, and then, compare it with tblAliasOriginal, to see the changes
I've made.
I have a datagrid to make changes to tblAlias and I delete a row using:

Dim dtAlias As DataTable
dtAlias = Session("tblAlias")
dtAlias.Rows(e.Item.ItemIndex).Delete()
dtAlias.AcceptChanges()

Session("tblAlias")
BindGrid()
My problem is that when I delete a row in tblAlias, that row is also deleted
from tblAliasOriginal! Why? How can I prevent this?
 
M

Marina Levit [MVP]

Both session variables are pointing to the same object. There is only ever
1 object in memory - and both tblAlias and tblAliasOriginal are pointing to
it.

I recommend you read up on object references, and what it is to be a
reference type versus a value type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top