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--
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--