Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6343463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:26:31+00:00 2026-05-24T20:26:31+00:00

I have a small issue accessing a byte[] : I have a binary object

  • 0

I have a small issue accessing a byte[]:
I have a binary object (byte[] saved to mssql db) which I get from the db and I want to read it. Whenever I access it, for its length or for its Read() method, I get a Cannot access a closed Stream exception.
What’s the best way to treat binaries if they have to be updated in the code and than saved again to the db?

Thanks.

Edit – code
In this application we convert a test object to a generic data object we’ve created to simplify, so this is the data object:

public class DataObject
{
    public Stream Content { get; set; }
    public Descriptor Descriptor { get; set; }
}

The descriptor contains metadata only (currently only name and description strings) and is not relevant, I think.
The test is more complicated, I’ll start by adding the mapping into data object. The serializer mentioned is NetDataContractSerializer.

public DataObject Map(Test test)
{
    using(var stream = new MemoryStream())
    {
        Serialize(test, stream);
        return new DataObject { Content = stream, Descriptor = test.Descriptor };
    }
}
 private void Serialize(Test test, MemoryStream stream)
 {
    serializer.WriteObject(stream, test);
    stream.Flush();
    stream.Position = 0;
 }

and vice versa:

public Test Build(DataObject data)
{
      using (var stream = data.Content)
      {
           var test = Deserialize(stream);
           test.Descriptor = data.Descriptor;
           return test ;
      }
}
private Test Deserialize(Stream stream)
{
    return serializer.ReadObject(stream) as IPythonTest;
}  

Edit II – trying to change the test’s content:
This is my first attempt handling streams, I’m not sure I’m doing it right, so I’ll explain first what I want to do: The information in data field should be saved into the test’s data object.

private static void UpdateTestObject(DataObject data, Test test)
{
    var testData = new byte[data.Content.Length];
    data.Content.Read(testData, 0, (int) data.Content.Length);
    test.TestObject = testData;
}

The exception is thrown in UpdateTestObject when accessing data.Content. I get it after creating some test, mapping it, and trying to save it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:26:32+00:00Added an answer on May 24, 2026 at 8:26 pm

    So the problem is caused by the Map() method – as far as I could understand, since it used:

    using (var stream = new MemoryStream())
    { ... }
    

    The stream was disposed of at the end of the block. Changing it to declaring a MemoryStream and then using it afterwards worked.
    Thanks to everyone who gave it a thought (not mentioning reading all this code)! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small issue. I am trying to pull some data from my
I have a small issue with a CA2000 warning. In my project which is
I have a small issue. Sometimes when I debug an application I want to
I am just new to programming in Unix and have a small issue that
can anyone help, i have a small issue, i have an interface and also
We're having a small issue and could use some help - we have the
everything is working fine with my jqgrid except a small issue. i have defined
have small problem, and would very much appreciate help :) I should convert byte
I have small page which has label, DropDownList and a submit button. <div> <asp:label
I have small problem with my .net 2.0 winforms application. I want to embed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.