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();
}
mike DelvottiPosted Jul 14, 2014, 8:14 AM
Thanks Vulpes,
I think that's pointed me in the right direction :)
VulpesPosted Jul 14, 2014, 7:13 AM
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 DelvottiPosted Jul 14, 2014, 6:53 AM
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?
VulpesPosted Jul 11, 2014, 11:41 AM
So you can assign them directly to a couple of cells in the DGV.
No need for Datasets or anything like that.
mike DelvottiPosted Jul 11, 2014, 11:30 AM
Thanks Vulpes,
So in theory I should populate that into dataset before filling a datagrid?
VulpesPosted Jul 11, 2014, 11:00 AM
So database.UserName and database.Title should give you the info you need.
mike DelvottiPosted Jul 11, 2014, 10:56 AM
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;
}
VulpesPosted Jul 11, 2014, 10:21 AM
What class of object does 'this' refer to in the serialization code?
mike DelvottiPosted Jul 11, 2014, 9:54 AM
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;
VulpesPosted Jul 11, 2014, 9:44 AM
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.