Hey,
I've a problem: I saved a gridlayout in a database as string. Then I want to load the layout again from the database. The load-function works for files and streams, so I tried to convert my string into a stream, but everytime I get the exception, that the stream has no correct binary format.
I've tried:
System.IO.TextReader tr = new StringReader(dr["layout"].ToString()); byte[] a = System.Text.Encoding.Unicode.GetBytes(tr.ReadToEnd()); System.IO.MemoryStream memStream = new System.IO.MemoryStream(a); this.ultraGrid1.DisplayLayout.Load(memStream);
and also:
System.IO.TextReader tr = new StringReader(dr["layout"].ToString()); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); StreamWriter sw = new StreamWriter(memStream); sw.Write(tr.ReadToEnd()); memStream.Seek(0, SeekOrigin.Begin); this.ultraGrid1.DisplayLayout.Load(memStream);
in both cases I get the exception.
I hope there is some help,
greetings
Stream to string
System.IO.MemoryStream msLayout = new System.IO.MemoryStream();Byte[] byteLayout = msLayout.ToArray();string strLayout = Encoding.UTF8.GetString(byteLayout);
string to stream
string strLayout;Byte[] byteRibbon = Encoding.UTF8.GetBytes(strLayout);System.IO.MemoryStream msLayout= new System.IO.MemoryStream(byteLayout);
Using an Encoder to convert the stream into a string is a really bad idea. The whole point of an Encoder is to manipulate the stream and coerce it into a string of a particular encoding type. Modifying the stream in this way is basically just asking for it to be damaged.
If you want to convert the stream into a string, a better way to do it would be to use the StreamReader class - and then use a StreamWriter to get it back into a stream.
Wow thanks - thats kinda easy.
Greetings