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