Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have some list<> objects in my application and i want to serialize these lists and insert them to the database , then i want to Deserialize the binary data fetched from the database to remake the list<> again


thanks

this is serialization part ::::
C#
MemoryStream departmanStream = new MemoryStream();
// Updated with two lines from comment reply
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(departmanStream, Departments);
departmanStream.Position = 0;
byte[] DB = departmanStream.ToArray();


here we see the error in Deserialize part :::::


C#
connection load = new connection("term_selectbyname", "name", TermToLoad);  

// get the binary codes from Database , connection is a class to connect
// to db through name of the query and the params
DataTable dt_load = new DataTable();
dt_load = load.senddt();  // send datatable from connection class and put it in the load dt
BinaryFormatter formatter = new BinaryFormatter();

string temp = dt_load.Rows[0][1].ToString();                                            
Stream departmanStream = GenerateStreamFromString(temp);
departmanStream.Position = 0;  
// Exception "End of Stream encountered before parsing was completed" occurs here.             
List<departman> dep = (List<departman>)formatter.Deserialize(departmanStream);  
grddepartman.DataSource = dep;




the function GenerateStreamFromString(string s) used above::::

C#
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
Posted
Updated 9-Sep-14 7:44am
v5
Comments
George Jonsson 9-Sep-14 12:56pm    
Where does it fail exactly?
Is connection your own class?
Saeed Jafarian 9-Sep-14 13:05pm    
in deserialize part line
:List<departman> dep = (List<departman>)formatter.Deserialize(departmanStream); exception : "End of Stream encountered before parsing was completed"

and i also think there is something wrong with my serialize part because when i see my database records all of the columns and rows are the same as each other despite the different list<> objects, the columns of the DB is varbinary(MAX) to save binary data
and yes the connection is the class of my own which i write to connect to the database with just giving name of the query and the parameters
George Jonsson 9-Sep-14 13:18pm    
But the only thing you do in your serialization part is to initialize an empty MemoryStream and then use ToArray(); ????
The length of the byte array, DB, should be 0.
Saeed Jafarian 9-Sep-14 13:22pm    
i am sorry
can you explain more about "The length of the byte array, DB, should be 0." ? what do you mean exactly?
George Jonsson 9-Sep-14 13:26pm    
This part. What is the data you expect the be filled into the variable DB?
MemoryStream departmanStream = new MemoryStream();
departmanStream.Position = 0;
byte[] DB = departmanStream.ToArray();

1 solution

By converting a varbinary column to a string, and then converting the string back to a series of bytes, you will end up corrupting the data.

Try reading the bytes directly:
C#
byte[] serializedData = (byte[])dt_load.Rows[0][1];
using (MemoryStream departmanStream = new MemoryStream(serializedData))
{
    List<departman> dep = (List<departman>)formatter.Deserialize(departmanStream); 
    grddepartman.DataSource = dep;
}
 
Share this answer
 
Comments
Saeed Jafarian 9-Sep-14 14:07pm    
now i get the error :
The input stream is not a valid binary format. The starting contents (in bytes) are: 53-00-79-00-73-00-74-00-65-00-6D-00-2E-00-42-00-79 ...

which i think there is something wrong with my serialize part, which i explain in the answer to your comment above
Richard Deeming 9-Sep-14 14:14pm    
There's nothing obviously wrong with the serialization code you've posted. However, the bytes in your database look like the Unicode representation of the string "System.Byte[]".

I'm guessing that the code which saves your serialized data to the database calls .ToString() on the byte array before saving it to the database. That makes me suspect that you're using string concatenation to build the query, rather than using a parameterized query.

Can you post the code which saves the serialized data to the database? (Use the "Improve question" link to add it to the question rather than trying to post it in a comment.)
Saeed Jafarian 9-Sep-14 15:02pm    
yeees you are right , in my insert query and my connection class the parameters are changed to string and that was the problem, so i fix them and the serialize part work as well which i should thank you very much , your answers was very helpful and i appreciate you

but now i get error in deserialize that means i cannot cast the right side to the left side
this line:
List<departman> dep =(List<departman>)formatter.Deserialize(departmanStream);

any ideas?
Richard Deeming 9-Sep-14 15:09pm    
Try storing the result of the Deserialize call in an object variable. Set a break-point on the line, and examine the type of the data.

object data = formatter.Deserialize(departmanStream); // <-- Breakpoint here
List<departman> dep = (List<departman>)data;
Saeed Jafarian 9-Sep-14 15:15pm    
thank you very much for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900