Storing a user created object as a session variable

  • Thread starter Thread starter RSH
  • Start date Start date
R

RSH

Hi,

I have a situation where I have created an object that contains
fields,properties and functions. After creating the object I attempted to
assign it to a session variable so i could retrieve the information it
contained on another page. This was significant because I am initially
loading the data from the database, then storing relevent information in the
object, I am allowing users to change the data then preview the
modifications on a secondary page...BEFORE they save the changes. Then if
they decide to save the changes they will go ahead and save them to a
database.

Everything seemed to be working fine until I attempted to store the object
in a session variable, when I received this error:
The error description is as follows : System.Web.HttpException: Unable to
serialize the session state. Please note that non-serializable objects or
MarshalByRef objects are not permitted when session state mode is
'StateServer' or 'SQLServer'. --->
System.Runtime.Serialization.SerializationException: The type
MyTimePlus.SBOContact in Assembly MyTimePlus, Version=1.0.2686.13333,
Culture=neutral, PublicKeyToken=null is not marked as serializable.



Is there anyway for me to store this object in a session variable? Or do I
have to split the class into two seperate classes, one that stores the
properties and the other that manages the data management? In that case do
I use inheritance, or just let them remain two independent objects that
communicate? ...or what is the recommended way to handle this scenerio?

ASP .Net 1.1 / Session: State Server

Thanks!

Ron



Public Class SBOContact

Private m_SBONumber As String

Private m_ContactName As String

Private m_URLName As String

Private m_Phone1 As String

Private m_Phone2 As String

Private m_Phone3 As String

Private m_Phone4 As String

Private m_Phone5 As String

Private m_Phone6 As String

Private m_Phone1Label As String

Private m_Phone2Label As String

Private m_Phone3Label As String

Private m_Phone4Label As String

Private m_Phone5Label As String

Private m_Phone6Label As String

Private m_Address1 As String

Private m_Address2 As String

Private m_City As String

Private m_State As String

Private m_ZipCode As String

Private m_Email As String

#Region "Constructor"

Public Sub New(ByVal SBONumber As String)

m_SBONumber = SBONumber

LoadData()

End Sub

#End Region

#Region "Properties"

Public Property SBONumber() As String

Get

Return m_SBONumber

End Get

Set(ByVal Value As String)

m_SBONumber = Value

End Set

End Property

Public Property ContactName() As String

Get

Return m_ContactName

End Get

Set(ByVal Value As String)

m_ContactName = Value

End Set

End Property

Public Property URLName() As String

Get

Return m_URLName

End Get

Set(ByVal Value As String)

m_URLName = Value

End Set

End Property

Public Property Phone1() As String

Get

Return m_Phone1

End Get

Set(ByVal Value As String)

m_Phone1 = Value

End Set

End Property

Public Property Phone2() As String

Get

Return m_Phone2

End Get

Set(ByVal Value As String)

m_Phone2 = Value

End Set

End Property

Public Property Phone3() As String

Get

Return m_Phone3

End Get

Set(ByVal Value As String)

m_Phone3 = Value

End Set

End Property

Public Property Phone4() As String

Get

Return m_Phone4

End Get

Set(ByVal Value As String)

m_Phone4 = Value

End Set

End Property

Public Property Phone5() As String

Get

Return m_Phone5

End Get

Set(ByVal Value As String)

m_Phone5 = Value

End Set

End Property

Public Property Phone6() As String

Get

Return m_Phone6

End Get

Set(ByVal Value As String)

m_Phone6 = Value

End Set

End Property

Public Property Phone1Label() As String

Get

Return m_Phone1Label

End Get

Set(ByVal Value As String)

m_Phone1Label = Value

End Set

End Property

Public Property Phone2Label() As String

Get

Return m_Phone2Label

End Get

Set(ByVal Value As String)

m_Phone2Label = Value

End Set

End Property

Public Property Phone3Label() As String

Get

Return m_Phone3Label

End Get

Set(ByVal Value As String)

m_Phone3Label = Value

End Set

End Property

Public Property Phone4Label() As String

Get

Return m_Phone4Label

End Get

Set(ByVal Value As String)

m_Phone4Label = Value

End Set

End Property

Public Property Phone5Label() As String

Get

Return m_Phone5Label

End Get

Set(ByVal Value As String)

m_Phone5Label = Value

End Set

End Property

Public Property Phone6Label() As String

Get

Return m_Phone6Label

End Get

Set(ByVal Value As String)

m_Phone6Label = Value

End Set

End Property

Public Property Address1() As String

Get

Return m_Address1

End Get

Set(ByVal Value As String)

m_Address1 = Value

End Set

End Property

Public Property Address2() As String

Get

Return m_Address2

End Get

Set(ByVal Value As String)

m_Address2 = Value

End Set

End Property

Public Property City() As String

Get

Return m_City

End Get

Set(ByVal Value As String)

m_City = Value

End Set

End Property

Public Property State() As String

Get

Return m_State

End Get

Set(ByVal Value As String)

m_State = Value

End Set

End Property

Public Property ZipCode() As String

Get

Return m_ZipCode

End Get

Set(ByVal Value As String)

m_ZipCode = Value

End Set

End Property

Public Property Email() As String

Get

Return m_Email

End Get

Set(ByVal Value As String)

m_Email = Value

End Set

End Property

#End Region



Private Sub LoadData()

Dim i As Integer

Dim cmdReader As SqlCommand

Dim dtrList As SqlDataReader

Dim j As Integer

Dim strSQL As String

Dim Con As SqlConnection

Con = New SqlConnection(AppSettings("SqlConn"))

Try

Con.Open()

strSQL = "SELECT * FROM Table WHERE SBONumber='" & m_SBONumber & "'"

cmdReader = New SqlCommand(strSQL, Con)

dtrList = cmdReader.ExecuteReader

While dtrList.Read

m_SBONumber = SetValue(dtrList("SBONumber"))

m_ContactName = SetValue(dtrList("ContactName"))

m_URLName = SetValue(dtrList("URLName"))

m_Phone1 = SetValue(dtrList("Phone1"))

m_Phone2 = SetValue(dtrList("Phone2"))

m_Phone3 = SetValue(dtrList("Phone3"))

m_Phone4 = SetValue(dtrList("Phone4"))

m_Phone5 = SetValue(dtrList("Phone5"))

m_Phone6 = SetValue(dtrList("Phone6"))

m_Phone1Label = SetValue(dtrList("Phone1Label"))

m_Phone2Label = SetValue(dtrList("Phone2Label"))

m_Phone3Label = SetValue(dtrList("Phone3Label"))

m_Phone4Label = SetValue(dtrList("Phone4Label"))

m_Phone5Label = SetValue(dtrList("Phone5Label"))

m_Phone6Label = SetValue(dtrList("Phone6Label"))

m_Address1 = SetValue(dtrList("Address1"))

m_Address2 = SetValue(dtrList("Address2"))

m_City = SetValue(dtrList("City"))

m_State = SetValue(dtrList("State"))

m_ZipCode = SetValue(dtrList("ZipCode"))

m_Email = SetValue(dtrList("Email"))

End While

If dtrList.IsClosed = False Then dtrList.Close()

Catch exc As Exception

dim errObject as ErrorObject

errObject .HandleError(exc)

Finally

If Con.State = ConnectionState.Open Then Con.Close()

Con = Nothing

End Try

End Sub

Private Function SetValue(ByVal Field) As String

If Not Field Is DBNull.Value Then

If Len(Trim(Field)) > 0 Then

Return Field

Else

Return ""

End If

Else

Return ""

End If

End Function

End Class
 
Yeah. Is the session state mode in your Web.config file set to "InProc"?
It must be for this to work.

I think.


Peter
 
When using Sql Server/In Proc session caching mechanism(s), the objects need
to be serialiable.
(with sql server, remember the object is being persisted to a database, so
it has to be serializable).


You might be interested in this:

10/24/2005
Web Session Wrapper for storing and retrieving objects
http://sholliday.spaces.live.com/blog/
 
Back
Top