This project has moved and is read-only. For the latest updates, please go here.

Calling GetAsByteArray and Save in the same Method

Jun 21, 2011 at 1:57 PM
Edited Jun 21, 2011 at 1:58 PM

I have a method that needs to save the excel file generated to the database using GetAsByteArray.

I also want to save the file to a location on disk.

Once I call Save on the package, I get the following error {"Package object was closed and disposed, so cannot carry out operations on this object or any stream opened on a part of this package."}

Is there a way around this?

Jun 22, 2011 at 7:45 PM

I suggest you save it to a variable and then save the byte array to a file and your db. Something like...

byte[] doc = package.GetAsByteArray();
File.WriteAllByte("c:\yourfile.xlsx", doc);
//code to save to sql...

Nov 21, 2011 at 8:44 PM

This behavior should be changed.  GetAsByteArray() should not dispose of the object.  This behavior is not implied in the name.  The function should do exactly what the name implies: return a Byte Array of the object.  If the developer wishes to dispose of the object they should have the option of calling the dispose method.

Alternatively rename the function GetAsByteArrayAndDispose();