473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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.BinaryForm atter
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--
Nov 19 '05 #1
2 7921
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
ce wrote:
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.BinaryForm atter
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--

Nov 19 '05 #2
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.BinaryForm atter
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.BinaryForm atter
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--
"Joshua Flanagan" <jo**@msnews.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
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
ce wrote:
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.BinaryForm atter
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--


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob Rock | last post by:
I already found an alternative way to accomplish this (using ReadBytes), still I'd like to understand why I'm getting and error reading a text file using the following method. The exception is...
2
by: Claus - Arcolutions | last post by:
I got a word document as a stream, and I want to get the text from the word document. But I cant seem to find anything to use for that purpose. The "Microsoft office ?.object" com reference, only...
2
by: CVerma | last post by:
I'm using an html input control (System.web.UI.HTMLControls.HTMLInputFile) to upload files such as msword, excel, jpg, and pdf. I have the encType property set in the form:...
6
by: Tom Kaminski [MVP] | last post by:
I can do this in ASP, but not sure how to handle in ASP.NET: How To Use the ADODB.Stream Object to Send Binary Files to the Browser through ASP http://support.microsoft.com/?kbid=276488 Do I...
1
by: blini | last post by:
This command is giving error. The error returned from the Javascript is: "the server of Automation cannot create object". How I arrange this error?
7
by: semedao | last post by:
Hi, I am using cryptostream on both sides on tcp connection that pass data. I am also use asyc socket , so , the data that recieved in the callback method not always have the length of the buffer...
5
by: chandanlinster | last post by:
When I was reading the man-page of fflush, I came across this statement --- "The function fflush forces a write of all user-space buffered data for the given output or update stream via the...
4
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, (sorry to begin with Java in a Python list ;-) in Java, when I want to pass input to a function, I pass "InputStream", which is a base class of any input stream. In Python, I found...
0
by: RobUK | last post by:
We have Apache on the local network managing several websites that are accessed using virtual domains (configured in the local machine's hosts file, eg. 192.168.1.10 points to dev.somedomain.com). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.