Guys, is it possible to Deserialize a binary text file and read it into a datagridview?
My below code does a rough job of trying to display it onto a message box but doesn't format it correctly
Ideally it would be nice to read the contents into a datagridview, is that possible?
FileStream readStream;
string msg = null;
try
{
readStream = new FileStream("c:\\DB.dat", FileMode.Open);
BinaryReader readBinary = new BinaryReader(readStream);
msg = readBinary.ReadString();
MessageBox.Show(msg);
readStream.Close();
}

VulpesRank #55 · 29.2K reputationPosted Jul 11, 2014, 5:44 AM EDT
If it was then it should be possible to deserialize it and bind to, or otherwise populate, the DGV with its elements.
Otherwise, you'd have to try and 'manually' format the string retrieved from the binary file into some sort of collection and populate the DGV from that.
mike DelvottiRank #7,298 · 244 reputationPosted Jul 11, 2014, 5:54 AM EDT
Hi Vulpes,
Yes, and I'm only really interested in getting the Username & Title sections?
It save's those details into binary from textfields, below is the code that writes the dat file:
FileStream fileStream =
new FileStream("DB.dat",FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream,this);
fileStream.Close();
return true;
VulpesRank #55 · 29.2K reputationPosted Jul 11, 2014, 6:21 AM EDT
What class of object does 'this' refer to in the serialization code?
mike DelvottiRank #7,298 · 244 reputationPosted Jul 11, 2014, 6:56 AM EDT
I'm not sure on that one, but below is the load code which outputs some activity onto a consol:
try
{
FileStream fileStream = new FileStream("c:\\DB.dat",FileMode.Open);
BinaryFormatter binaryFormatter = new BinaryFormatter();
Database temp = (Database)binaryFormatter.Deserialize(fileStream);
this.database=temp.database;
fileStream.Close();
return true;
}
catch
{
database = new Hashtable();
offlineMessages=new ArrayList();
return false;
}
VulpesRank #55 · 29.2K reputationPosted Jul 11, 2014, 7:00 AM EDT
So database.UserName and database.Title should give you the info you need.
mike DelvottiRank #7,298 · 244 reputationPosted Jul 11, 2014, 7:30 AM EDT
Thanks Vulpes,
So in theory I should populate that into dataset before filling a datagrid?
VulpesRank #55 · 29.2K reputationPosted Jul 11, 2014, 7:41 AM EDT
So you can assign them directly to a couple of cells in the DGV.
No need for Datasets or anything like that.
mike DelvottiRank #7,298 · 244 reputationPosted Jul 14, 2014, 2:53 AM EDT
Hi Vulpes,
I think I'm getting closer with reading the file, for now I'm just trying to read it to a message box but I get an error thrown on the below code?
Database temp = (Database)binaryFormatter.Deserialize(fileStream);
The error is:
System.Runtime.Serialization.SerializationException was unhandled
Message="The input stream is not a valid binary format. The starting contents (in bytes) are: 20-01-20-20-20-FF-FF-FF-FF-01-20-20-20-20-20-20-20 ..."
Any ideas?
VulpesRank #55 · 29.2K reputationPosted Jul 14, 2014, 3:13 AM EDT
The error message suggests that the file pointer is not at the beginning of the stream which it needs to be for deserialization to work.
mike DelvottiRank #7,298 · 244 reputationPosted Jul 14, 2014, 4:14 AM EDT
Thanks Vulpes,
I think that's pointed me in the right direction :)