Deserializing from string

C

Chris

I need to serialise a business object into a session as a string. The reason
for this is that we have to run session state with classic ASP and the code
we use in effect mean ASP.net sessions can only handle strings. My initial
thought is to serialize the object into a string and put it into the session
but I am having problem deserializing it.

Code to serialise
login = New login.clsLogin
Dim objStream As New MemoryStream
Dim objFormatter As New BinaryFormatter

objFormatter.Serialize(objStream, login)
Session("login") = login

Code to deserialise

Dim objFormatter As New BinaryFormatter
Dim enc As New UTF8Encoding
Dim arrBytData() As Byte = enc.GetBytes(Session("login").ToString)
Dim ms As MemoryStream = New MemoryStream()
ms.Write(arrBytData, 0, arrBytData.Length)
ms.Seek(0, 0)
login = objFormatter.Deserialize(ms)

The error I get is:

End of Stream encountered before parsing was completed.

Can anyone point me in the right direction. Regards, Chris.
 
C

Chris

Figured it out

Dim MyFormatter As New BinaryFormatter()
Dim ms As New MemoryStream
MyFormatter.Serialize(ms, login)

Dim bytearray As Byte() = ms.ToArray()
Dim str As String = Convert.ToBase64String(bytearray)
Session("login") = str


Dim objFormatter As New BinaryFormatter
Dim GenericObject As Object
Dim arrBytData() As Byte =
Convert.FromBase64String(Session("login").ToString)
Dim ms As MemoryStream = New MemoryStream(arrBytData)

ms.Write(arrBytData, 0, arrBytData.Length)
ms.Seek(0, SeekOrigin.Begin)
GenericObject = objFormatter.Deserialize(ms)
login = CType(GenericObject, login.clsLogin)
 

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

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,481
Latest member
Arnulfo32P

Latest Threads

Top