BinaryFormatter formatter = new BinaryFormatter();
    using (MemoryStream m = new MemoryStream())
    {
        formatter.Serialize(m, list);
        StreamReader sr = new StreamReader(m);
        HiddenField1.Value = sr.ReadToEnd();
    }
i'm getting a blank value for HiddenField1.Value. Not sure what I'm doing is even possible? list is definitely populated (is a List<T>)
Depending on what you want to achieve... One option is to show content of the binary stream as Base64 string:
var memoryStream = new MemoryStream();
using(memoryStream)
{
    formatter.Serialize(memoryStream, list);
}
HiddenField1.Value = Convert.ToBase64String(memoryStream.ToArray());
                        Change it to:
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream m = new MemoryStream())
{
    formatter.Serialize(m, list);
    m.Position = 0;
    StreamReader sr = new StreamReader(m);
    HiddenField1.Value = sr.ReadToEnd();
}
You need to reset the position of the stream back to the beginning before reading it.  Also, you shouldn't use StreamReader to convert a binary stream like this to text, because it will break in unexpected ways.  If you want the results in a text-like format, use Convert.ToBase64String as in @Alexei's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With