locked
Exception of type 'System.OutOfMemoryException' was thrown. RRS feed

  • Question

  • User566650967 posted

    Hi,

    I have got a error exception  as "Exception of type 'System.OutOfMemoryException' was thrown." while using the following function to fill images in dataset into crystal report.

    The error is breaking at byte[] ImgData = new byte[stream.Length];

    private void DisplayImages(DataRow row, string Img, string path)

    {

    FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);

    byte[] ImgData = new byte[stream.Length];

    stream.Read(ImgData, 0, Convert.ToInt32(stream.Length));

    stream.Close();

    row[Img] = ImgData;

    }

    Please do provide me solution ASAP

    Saturday, January 4, 2014 5:47 AM

Answers

  • User1563932795 posted

    Try putting the FileStream in a using statement like this:

            private void DisplayImages(DataRow row, string Img, string path)
            {
    
                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    byte[] ImgData = new byte[stream.Length];
    
                    stream.Read(ImgData, 0, Convert.ToInt32(stream.Length));
    
                    stream.Close();
    
                    row[Img] = ImgData;
                }
            }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 4, 2014 5:45 PM

All replies

  • User1563932795 posted

    Try putting the FileStream in a using statement like this:

            private void DisplayImages(DataRow row, string Img, string path)
            {
    
                using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    byte[] ImgData = new byte[stream.Length];
    
                    stream.Read(ImgData, 0, Convert.ToInt32(stream.Length));
    
                    stream.Close();
    
                    row[Img] = ImgData;
                }
            }
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 4, 2014 5:45 PM
  • User566650967 posted

    it worked awesome . Darren Thanks

    Sunday, January 5, 2014 3:27 AM
  • User566650967 posted

    Hi Darren,

    When we select more than 50 in count while adding in byte array "system.memory exception" occurs. Could you let me know a solution

    Monday, January 6, 2014 3:28 AM
  • User1563932795 posted

    Hi Darren,

    When we select more than 50 in count while adding in byte array "system.memory exception" occurs. Could you let me know a solution

    Hi Ron, it looks like you are now running out of server memory.  The simplest would be to not let them select more than 50.  If you need to allow selecting more than 50 then your possible options are in the RESOLUTION section in this link http://support.microsoft.com/kb/909678.

    Monday, January 6, 2014 5:39 PM