473,564 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim memStream As New System.IO.Memor yStream
'Serialize object; store in viewstate.
BinFormatter.Se rialize(memStre am, myBizObj)
viewstate("BizO bj") = memStream
memStream.Close ()
memStream = Nothing

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

--end of code sample--
Nov 19 '05 #1
2 7952
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.T oArray()), 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.W rite()), 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.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim memStream As New System.IO.Memor yStream
'Serialize object; store in viewstate.
BinFormatter.Se rialize(memStre am, myBizObj)
viewstate("BizO bj") = memStream
memStream.Close ()
memStream = Nothing

'Deserialize object; put back in myBizObj.
Dim memStream As New System.IO.Memor yStream
memStream = CType(viewstate ("BizObj"), System.IO.Memor yStream)
myBizObj = CType(BinFormat ter.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.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim memStream As New System.IO.Memor yStream
BinFormatter.Se rialize(memStre am, MyBizObj)
viewstate("BizO bj") = memStream.ToArr ay()
memStream.Close ()
memStream = Nothing

'*** Deserialize
Dim BinFormatter As New
Runtime.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim bytViewState() As Byte = DirectCast(view state("BizObj") , Byte())
Dim memStream2 As New System.IO.Memor yStream(bytView State)
MyBizObj = DirectCast(BinF ormatter.Deseri alize(memStream 2), BizObj)
--end of code sample--
"Joshua Flanagan" <jo**@msnews.co m> wrote in message
news:%2******** *******@TK2MSFT NGP14.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.T oArray()), 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.W rite()), 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.Seriali zation.Formatte rs.Binary.Binar yFormatter
Dim memStream As New System.IO.Memor yStream
'Serialize object; store in viewstate.
BinFormatter.Se rialize(memStre am, myBizObj)
viewstate("BizO bj") = memStream
memStream.Close ()
memStream = Nothing

'Deserialize object; put back in myBizObj.
Dim memStream As New System.IO.Memor yStream
memStream = CType(viewstate ("BizObj"), System.IO.Memor yStream)
myBizObj = CType(BinFormat ter.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
23518
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 returned on the ReadString call. public string BinaryRead(string fileName) { FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read,...
2
3171
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 include functionality to read from a file (as far as I know). I looked a little on the structure of the document, but it doesnt seem to have any...
2
15470
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: encType="multipart/form-data" <INPUT id="UploadFile" type="file" name="UploadFile" runat="server"> Private Sub btnUploadFile_Click(ByVal sender As System.Object,...
6
4467
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 need to use BinaryReader? Can someone give me an example?
1
9752
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
13355
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 I sent. for ex I want to send 10000 bytes I can get it in 10 times of 1000 bytes each. so , I need to know when I complete the receiving , I want...
5
3217
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 stream's underlying write function." what exactly is an "update stream"?
4
3746
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 that "file" objects exist. While specifying argument types in Python is not possible as in Java, it is possible to check whether an object is an...
0
2196
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). I am reworking the way that I access images on the site by specifying the http://etc address of the image instead of the local relative address. This...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.