Click here to Skip to main content
13,006,705 members (61,582 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hi all,

i got a below error in my application, please find out this bugs and let me know,

my error is:
"Index was outside the bounds of the array."

my error content is:
while (reader.Read())
       //  pubID = reader.GetString(0);
         stream =
         new FileStream("abcd.docx", FileMode.OpenOrCreate, FileAccess.Write);
         writer = new BinaryWriter(stream);
         startIndex = 0;
         retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
         while (retval == bufferSize)
             startIndex += bufferSize;
             retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
         // the above line i got the mentioned error.
         writer.Write(outByte, 0, (int)retval - 1);


thanks to advance.
Posted 16-Jan-13 4:31am
David_Wimbley 16-Jan-13 10:46am
Stellus said they are getting the error at this line

retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);
// the above line i got the mentioned error.
stellus 16-Jan-13 11:24am
Nick Fisher (Consultant) 16-Jan-13 10:55am
What has 'bufferSize' been initialised to, and how did you determine this value? The first line that reads the file uses this variable, but there's nothing in the code to suggest where you got this value from?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You don't provide the rest of the code so I have to guess that reader is a SqlDataReader class based on the structure of your code. If this is the case, you should refer to[^] for proper usage of the GetBytes method.

But, assuming I'm correct, it looks like the problem is your understanding of the parameters. GetBytes will attempt to read the number of bytes specified in the bufferSize variable. In your first read, you read bufferSize number of bytes from the stream. Assuming there are, at least, bufferSize + 1 bytes in the stream, you will go into the internal read loop requesting bufferSize number of bytes. If there is less than bufferSize number of bytes left, you are attempting to read past the end of the buffer... which is why you are getting the error.

If you read the notes in that link, you will see that if you pass a null output buffer, you will get back the total number of bytes in the stream. You could probably make all of this a lot easier by calling:

// Get the total number of bytes
retval = reader.GetBytes(1, 0, null, 0, 0);
byte outBytes[retval];
// Read the entire stream into outBytes
retval = reader.GetBytes(1,0,outBytes,0,retval);

There really is no reason to break the read up into multiple passes unless it was a massive binary blob that would be too big for the heap to allocate the outBytes array. Instead, get the size of the stream, reallocate the output array, fill it with all the data in one pass, and write it out to disk.

And on a side note, you don't really want to open and close your file handle for each new row... You should move that logic to outside the while loop. It will greatly increase the performance of the loop.

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.170628.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2017
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