I have a data table that is formatted from an array of Column Names, I.e.
DataTable ThisTable = new DataTable();
for (int i = 0; i <= col.GetUpperBound(0); i++)
{
try
{
ThisTable.Columns.Add(new DataColumn(col[i].ToString(), typeof(string)));
}
catch (Exception e)
{
MessageBox.Show("Uploader Error"+e.ToString());
return null;
}
}
i then use a BinaryFormatter
object to Serialize/Deserialize a memory stream so that I can read the data from the stream into a table. Essentially users will be uploading an excel sheet that will be read into a database. Although I realize that it is possible, I DO NOT want to write the sheet to disk then read it in.
However when I go to retrieve the datatable from the Deserialize()
call, I dont get an error, however I get an empty DataSet.
Mem would be my memory stream object.
BinaryFormatter bformat = new BinaryFormatter();
bformat.Serialize(Mem, ThisTable);
Mem.Seek(0, SeekOrigin.Begin);
byte[] bt = Mem.ToArray();
Stream s = new MemoryStream(bt);
Mem.Position = 0;
s.Position = 0;
ThisTable = (DataTable)bformat.Deserialize(Mem);
DS.Tables.Add(ThisTable);
return DS;