Click here to Skip to main content
12,445,482 members (59,378 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
if (videoData.Read())
    {
        byte[] a = (byte[])videoData[0];
        using (var videoStream = new MemoryStream(a))
        {
            Response.BufferOutput = false;
            byte[] buffer = new byte[400000];
            int bytesRead = 0;
            while ((bytesRead = videoStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                Response.OutputStream.Write(buffer, 0, bytesRead);
            }
        }
        videoData.Close();
    }
Posted 18-Sep-12 0:42am
Updated 18-Sep-12 1:48am
RyanDev217.3K
v3
Comments
Richard MacCutchan 18-Sep-12 8:05am
   
Use less memory or buy more; those are the choices.
Manfred R. Bihy 18-Sep-12 8:34am
   
The problem with your code is that you seem to be reading the whole BLOB at one time. This will most certainly exahaust your memory quickly if the BLOBs are very large or if there are many requests at one time. Since I can't see the code you're using to open the database I suggest you see my solution and try accessing the data in a sequential manner only reading smaller chunks of data and writing that to the response stream.
jerinkjohn 18-Sep-12 8:37am
   
how to read blob data ???
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

What if you move the
byte[] buffer = new byte[400000];
line, that is:

if (videoData.Read())
    {
        byte[] a = (byte[])videoData[0];
        byte[] buffer = new byte[400000]; // <--- moved here
        using (var videoStream = new MemoryStream(a))
        {
            Response.BufferOutput = false;
            int bytesRead = 0;
            while ((bytesRead = videoStream.Read(buffer, 0, buffer.Length)) &gt; 0)
            {
                Response.OutputStream.Write(buffer, 0, bytesRead);
            }
        }
        videoData.Close();
    }

?
  Permalink  
Comments
jerinkjohn 18-Sep-12 8:31am
   
not getting ...still it is showing outofmemory exception.
CPallini 18-Sep-12 8:32am
   
What is the offending line?
jerinkjohn 18-Sep-12 8:35am
   
den also it is showing error.
CPallini 18-Sep-12 8:38am
   
Yes, I did understand, now: at what line does the error occurr?
Manfred R. Bihy 18-Sep-12 8:39am
   
Hi CPallini!
The problem OP is encountering lies in the fact that he's trying to read in the whole BLOB at once. There is a method where one can use CommandBehavior.SequentialAccess to read the BLOB chunk wise. :)
CPallini 18-Sep-12 8:51am
   
Ah OK, it is the internal memory allocation of the VideoData reader. I supposed the OP was using too much memory prior of the garbage collector intervention.
I didn't know the DataReader was a bit 'dumb'.
jerinkjohn 19-Sep-12 0:59am
   
thnkzzzz........
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Please follow the sample at this site: Read / Write BLOBs from / to SQL Server using C# .NET DataReader [^]. Be on the lookout for this here phrase, as it's very essential to the correct function of the sample code: CommandBehavior.SequentialAccess.

The sample method of interest to you for the BLOB reading part has this signature:
public void GetEmployee(string plastName,string pfirstName)

Regards,

— Manfred
  Permalink  
Comments
jerinkjohn 18-Sep-12 8:32am
   
can you give one exaple program ???
Manfred R. Bihy 18-Sep-12 8:37am
   
You're kidding me! X|
1. The site contains a complete sample
2. There's even a download link at the top of that page where you can get the complete codes.
I ask you only once: "What more do you need?"
jerinkjohn 19-Sep-12 0:59am
   
thankzzz
Marcus Kramer 18-Sep-12 9:30am
   
+5. As much for the comment as the answer itself. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

The maximum capacity of a MemoryStream object is 512 MB on 32bit systems and 2 GB on 64bit systems, resp. See also http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/1af59645-cdef-46a9-9eb1-616661babf90[^].
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 19 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100