473,399 Members | 3,302 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,399 software developers and data experts.

Serialization and Deserialization

Hi all and thanks in advance for your help,

I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database (when I check the database the field that holds the binary data seems populated), but when I want to deserialize this same byte array the application fails and gives me the following error:

Binary stream does not contain a valid BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and deserialization.

Here is the code that I am using for serialization:

//Serialization of the FilterManager object and saving the binary data in the database
public int SaveFilterUser (int userID, string filterName, FilterManager mgr)
{
DatabaseContext objContext = null;
try
{
// Serialization
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms=new MemoryStream();
bf.Serialize(ms,mgr);
ms.Seek(0,0); // Return stream to start ?
ms.Position = 0; //Return to start
byte [] byteArray = ms.ToArray();
ms.Flush();
ms.Close();

//The rest of the Code here basically saves the byteArray generated to the database
FilterUser filterUser = new FilterUser();
filterUser.UserID = userID;
filterUser.SavedFilterName = filterName;
filterUser.FilterObjects = byteArray;
objContext = new DatabaseContext(false, strConn, strProvider);
FiltersUsersDalc objDA = new FiltersUsersDalc();
return objDA.Create (objContext, filterUser);
}
finally
{
if(objContext != null)
objContext.ReleaseConnection();
}
}

---------------------------------------------------------------------------------------------------
Here is the deserialization code:

///Deserialization of FilterManager object
public FilterManager GetFilterUser (int userID, string filterName)
{
DatabaseContext objContext = null;
try
{
//Get the binary data from the database based on username and filtername

objContext = new DatabaseContext(false, strConn, strProvider);
FiltersUsersDalc objDA = new FiltersUsersDalc();
FilterUser filterUser = objDA.GetFilterUser (objContext, userID, filterName);

//Basically Gets the byte Array back from the database and try to generate the Memory stream from it.
byte[] byteArray = filterUser.FilterObjects;
//Deserialize object
BinaryFormatter bf = new BinaryFormatter();
Stream ms=new MemoryStream(byteArray);
ms.Position = 0;
FilterManager mgr = (FilterManager) bf.Deserialize(ms); //###THE CODE FAILS HERE and returns the error message above.
ms.Flush();
ms.Close();
return mgr;
}
finally
{
if(objContext != null)
objContext.ReleaseConnection();
}
}

What am I doing wrong?

Any suggestions is appreciated,

Thanks,

Amadelle

One more thing: I am using the following code to get and set the binary data in the database (ofcourse I am not showing the whole code but how I set the parameters and how I get the results back)
To Get:
int bufferSize = 100;

byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes

long retval; // The bytes returned from GetBytes.

long startIndex = 0;

retval = objDataReader.GetBytes((int)ColumnIndex.FilterObje cts, startIndex, outbyte, 0, bufferSize);
// Continue reading while there are bytes beyond the size of the buffer.

while (retval == bufferSize)

{

// Reposition the start index to the end of the last buffer and fill the buffer.

startIndex += bufferSize;

retval = objDataReader.GetBytes((int)ColumnIndex.FilterObje cts, startIndex, outbyte, 0, bufferSize);

}

objFilterUser.FilterObjects = outbyte;

To Set:

aryParamList.Add("@FilterObjects", DbType.Binary, 50, objFilterUser.FilterObjects);

Nov 16 '05 #1
3 9776
Hi Amadelle,
Hi all and thanks in advance for your help,

I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database (when I check the database the field that holds the binary data seems populated), but when I want to deserialize this same byte array the application fails and gives me the following error:

Binary stream does not contain a valid BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and deserialization.

Here is the code that I am using for serialization:
The serialization & deserialization code is ok, but ...

One more thing: I am using the following code to get and set the binary data in the database (ofcourse I am not showing the whole code but how I set the parameters and how I get the results back)
To Get:
int bufferSize = 100;

byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes

long retval; // The bytes returned from GetBytes.

long startIndex = 0;

retval = objDataReader.GetBytes((int)ColumnIndex.FilterObje cts, startIndex, outbyte, 0, bufferSize);
// Continue reading while there are bytes beyond the size of the buffer.

that's wrong. use while (retval > 0 && retval <= bufferSize)
while (retval == bufferSize)

{

// Reposition the start index to the end of the last buffer and fill the buffer.

startIndex += bufferSize;
here startIndex += retval.

retval = objDataReader.GetBytes((int)ColumnIndex.FilterObje cts, startIndex, outbyte, 0, bufferSize);

}

objFilterUser.FilterObjects = outbyte;

To Set:

aryParamList.Add("@FilterObjects", DbType.Binary, 50, objFilterUser.FilterObjects);


bye
Rob
Nov 16 '05 #2
Ok this was a good suggestion, still didn't solve my problem though ..
I think you are absolutely right though Robert in the fact that the part
that I am messing up is getting the binary data from the database.
Several things that I noticed:
1- When I serialize the object the byte array that I generate has a size
(length) of 2700+. But when I get the array of bytes back it is only the
buffer size of 100
so the outbyte is never larger than the 100 which makes sense since I have a
line of code byte[] outbyte = new byte[buffersize] and buffersize being 100
explains why the lengths are never the same. So i tried to fix this problem
by using the buffer size of 8000. But even extending the buffer size didn't
solve my problem. So given my code, it seems that I am still not populating
the array of bytes correctly (I used your code change suggestions but that
didn't solve my problem either).
2- Next step .. I noticed that all of the array items in the byte array
generated after the serialization usually have a value in the form of number
associated with them. so for example arrBytes[2] has a value of 255. But
when I get the array back regardless of the size (100 vs. 2700), even the
first 100 all come back with values of 0. Further, in the code, I use a
variable called retval which is supposed to represent the bytes returned
from the GetBytes method of datareader. This retval always comes back with
a value of 1. So the loop for going from retval to buffer size pretty much
stops after the first round.

In the database I am using varbinary to store this binary data, and I know
that the size of this field is only 8000 bytes max. So here is the part
that I get fuzzy. I am not so sure whether my data gets cut down due to the
field limitations in SQL .... or am I doing something else wrong in the
calls that I make to read the data back from the database?

This is too complicated and I don't really understand the binary data and
how the sizes work in relation to the byte array, data type var binary, how
to read it back, etc. So any help would be great... if someone can also
direct me to the right location, documentation to read up on the issue as
well, that would be great too..

Thanks again in advance,

Amadelle
"Robert Jordan" <ro*****@gmx.net> wrote in message
news:ck*************@news.t-online.com...
Hi Amadelle,
Hi all and thanks in advance for your help,

I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in
the database (when I check the database the field that holds the binary data
seems populated), but when I want to deserialize this same byte array the
application fails and gives me the following error:
Binary stream does not contain a valid BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and
deserialization.
Here is the code that I am using for serialization:


The serialization & deserialization code is ok, but ...

One more thing: I am using the following code to get and set the binary data in the database (ofcourse I am not showing the whole code but how I set
the parameters and how I get the results back) To Get:
int bufferSize = 100;

byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes
long retval; // The bytes returned from GetBytes.

long startIndex = 0;

retval = objDataReader.GetBytes((int)ColumnIndex.FilterObje cts, startIndex, outbyte, 0, bufferSize);

// Continue reading while there are bytes beyond the size of the buffer.


that's wrong. use while (retval > 0 && retval <= bufferSize)
while (retval == bufferSize)

{

// Reposition the start index to the end of the last buffer and fill the buffer.
startIndex += bufferSize;


here startIndex += retval.

retval = objDataReader.GetBytes((int)ColumnIndex.FilterObje cts, startIndex, outbyte, 0, bufferSize);
}

objFilterUser.FilterObjects = outbyte;

To Set:

aryParamList.Add("@FilterObjects", DbType.Binary, 50, objFilterUser.FilterObjects);


bye
Rob

Nov 16 '05 #3
Hi Amadelle,
This is too complicated and I don't really understand the binary data and
how the sizes work in relation to the byte array, data type var binary, how
to read it back, etc. So any help would be great... if someone can also
direct me to the right location, documentation to read up on the issue as
well, that would be great too..


I never used the DB-stuff of the framework. However, the
usual idiom for pulling data from a reader is:

MemoryStream m = new MemoryStream();
byte[] buffer = new byte[8192];
long read;
long index = 0;

while ((read = objDataReader.GetBytes((int)ColumnIndex.FilterObje cts,
index, buffer, 0, buffer.Length) > 0)
{
m.Write(buffer, 0, (int)read);
index += read;
}

bye
Rob
Nov 16 '05 #4

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

Similar topics

2
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
4
by: Jeff T. | last post by:
Hello, I have an existing set of C# classes that encapsulate our application data. They are in a heirachy with each subclass defining more specific types of data. I would like to serialize these...
1
by: Maheal | last post by:
I have been trying to Serialize an object that is the child of another object which is also serializable. Here is the simplified scenario (assume missing code is correct): class One :...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
0
by: Goethals Frederik | last post by:
Hi, I have some questions that are a little difficult to explain, so I give it a try... I have an application (aSP.NET with VB.NET codebehind) and I would like to store my data on disk...
8
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
1
by: Rucha | last post by:
We are using ACAServices in our project, and are passing entity classes as parameters to the ACAServiceMethod. We are using a private variable entityState to indicate whether the entity is...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
3
by: Zachary Turner | last post by:
Hello, I have a situation where I would like to perform custom serialization and deserialization of an existing .NET framework object (specifically, System.DateTime). Is there a common paradigm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.