"Stream was not readable" error when deserializing object from viewstate

C

ce

Being a newbie regarding serialization and memorystreams, I was trying to
see if i could improve page performance (avoiding going to the db on a
postback) by saving my serialized business object in viewstate and getting
it back from the client on a postback. But the last line of the sample code
below throws a "Stream was not readable" error when i'm trying to get the
serialized object back out of the viewstate and memorystream.

Any suggestions?

Thanks,

Chris
--
<Serializable()> _
Public Class BizObj
Public ProjectFK As Integer
Public Title As String
Public Descr As String
...
End Class

dim myBizObj as BizObj
.... instantiate and populate myBizObj ...

Dim BinFormatter As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim memStream As New System.IO.MemoryStream
'Serialize object; store in viewstate.
BinFormatter.Serialize(memStream, myBizObj)
viewstate("BizObj") = memStream
memStream.Close()
memStream = Nothing

'Deserialize object; put back in myBizObj.
Dim memStream As New System.IO.MemoryStream
memStream = CType(viewstate("BizObj"), System.IO.MemoryStream)
myBizObj = CType(BinFormatter.Deserialize(memStream), BizObj) 'throws
"Stream was not readable" error.

--end of code sample--
 
J

Joshua Flanagan

I'm surprised that is your only problem, but that may be because I am
unfamiliar with how "loose" VB.NET will let you be with your types.

When you save the MemoryStream, you should probably explicitly convert
it to an array of bytes (MemoryStream.ToArray()), and store the array in
ViewState.

Then, when you retrieve the value from ViewState, cast it to an array of
bytes, write the bytes into a memorystream (MemoryStream.Write()), set
the Position property of the memorystream to 0, and then deserialize
from the memorystream.


Joshua Flanagan
http://flimflan.com/blog
 
C

ce

Joshua,

Thanks so much! That works great.

I'm still unsure if i should use it (or go back to the database), but at
least i know how now (brown cow). It adds 7k to my page so i have to decide
if that extra load every time (even for viewing a page) is worth the price
of faster postbacks (for saves).

Anyway, the modified vb.net code is shown below for anyone else who needs
it.

Thanks again!

Chris
--
'*** Binary Serialization
Dim BinFormatter As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim memStream As New System.IO.MemoryStream
BinFormatter.Serialize(memStream, MyBizObj)
viewstate("BizObj") = memStream.ToArray()
memStream.Close()
memStream = Nothing

'*** Deserialize
Dim BinFormatter As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim bytViewState() As Byte = DirectCast(viewstate("BizObj"), Byte())
Dim memStream2 As New System.IO.MemoryStream(bytViewState)
MyBizObj = DirectCast(BinFormatter.Deserialize(memStream2), BizObj)
--end of code sample--
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top