DanG
9/26/2006 6:17:00 PM
Got it!
If Session.Item("NewTransactions") Is Nothing Then
OldTransactions =
CType(Session.Item("OldTransactions"), ArrayList)
For intRow = 0 To OldTransactions.Count - 1
NewTransactions.Add(OldTransactions(intRow).clone())
Next
Else
NewTransactions =
CType(Session.Item("NewTransactions"), ArrayList)
End If
DanG wrote:
> Hi all
>
> Per the code below, I load an array from a session variable. When the
> user clicks Save, I attempt to build a new array that looks like the
> original, except with the updates from the screen. Then I compare the
> two, to see if any changes need to be written out to the database.
>
> Session.Item("OldTransactions") contains the original data.
> Session.Item("NewTransactions") is supposed to be the updated values.
> The problem is with the line
> newTranInfo.aField =
> CType(grdTransactions.Items(intRow).FindControl("txtField"),
> TextBox).Text
> in saveNewTransactions(). As soon as it executes, the value in
> Session.Item("NewTransactions") changes. Unfortunately,
> Session.Item("OldTransactions") also changes. Apparently, they're
> pointing to the same location in cache, but I don't understand why.
>
> Could someone please shed some light on this for me? What should I do
> differently?
>
> TIA
> Dan
>
>
> Private Sub loadTransactions()
> Dim transactionController = New TransactionController
>
> Dim arrTransactions As ArrayList = New ArrayList
>
> arrTransactions = transactionController.GetTransactions()
> Session.Item("OldTransactions") = arrTransactions
> end sub
>
>
> Private Sub saveNewTransactions()
>
> Dim tranController As TransactionController = New
> TransactionController
> Dim NewTransactions As New ArrayList
> Dim OldTransactions As New ArrayList
> Dim newTranInfo As TransactionInfo
>
> Try
> NewTransactions = CType(Session.Item("NewTransactions"),
> ArrayList)
> If NewTransactions Is Nothing Then
> OldTransactions =
> CType(Session.Item("OldTransactions"), ArrayList)
> NewTransactions = OldTransactions.Clone()
> End If
>
> For intRow = 0 To grdTransactions.Items.Count - 1
>
> newTranInfo = NewTransactions(intRow)
> newTranInfo.aField =
> CType(grdTransactions.Items(intRow).FindControl("txtField"),
> TextBox).Text '<<< HERE
>
> Next
>
> Catch ex As Exception
> showMessage("Error: " & ex.Message)
>
> End Try
>
> End Sub
>
> Private Sub saveEdits()
>
> Dim tranController As TransactionController = New
> TransactionController
> Dim oldTransactions As ArrayList
> Dim newTransactions As ArrayList
> Dim newTranInfo As TransactionInfo
> Dim oldtranInfo As TransactionInfo
> Dim strInvalidDataMsg As String = ""
> Dim intRow As Integer
> Dim chk As CheckBox
>
> Try
> oldTransactions = CType(Session.Item("OldTransactions"),
> ArrayList)
> newTransactions = CType(Session.Item("NewTransactions"),
> ArrayList)
>
> For intRow = 0 To grdTransactions.Items.Count - 1
>
> oldtranInfo = oldTransactions(intRow)
> newTranInfo = newTransactions(intRow)
>
> If Not newTranInfo.Equals(oldtranInfo) Then
>
> tranController.UpdateTransaction()
> oldTransactions(intRow) = newTranInfo.Clone()
>
> ' Reset the grid row data item
> grdTransactions.Items(intRow).DataItem =
> newTranInfo
> End If
>
> Next
>
> ' Replace the cached records
> Session.Item("OldTransactions") = oldTransactions
>
>
> Catch ex As Exception
> showMessage("Error: " & ex.Message)
>
> End Try
>
> End Sub