473,656 Members | 2,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Byte array truncates my memorystream

Hi,
I'm trying to download a memorystream to a .csv file on the users desktop.
When I go to write out the memorystream through the byte array it seems to
truncate at 1024 and I don't know how to increase the size. Here is my code
if that helps.

Thanks.

Dim msw As MemoryStream = New MemoryStream
msw.SetLength(5 000)

Dim sw As New StreamWriter(ms w)

' First we will write the headers.
Try
Dim strBuffer As String = ""
Dim c As Integer
Dim dt As DataTable = CType(Session(" GridData"), DataTable)
Dim iColCount As Integer = dt.Columns.Coun t

For c = 0 To iColCount - 1
strBuffer += dt.Columns(c).T oString

If (c < iColCount - 1) Then
strBuffer += ","
End If
Next

strBuffer += ControlChars.Ne wLine

' Now write all the rows.
Dim dr As DataRow
For Each dr In dt.Rows

For c = 0 To iColCount - 1
strBuffer += (dr(c).ToString )

If (c < iColCount - 1) Then
strBuffer += (",")
End If

Next
strBuffer += ControlChars.Ne wLine
Next
sw.Write(strBuf fer)

Dim byteArray() As Byte = msw.ToArray()

msw.Flush()
msw.Close()
Response.Clear( )

'Add Headers to enable dialog display
Response.AddHea der("Content-Disposition", "attachment ;
filename=TownGr ants.csv")
Response.AddHea der("Content-Length", byteArray.Lengt h.ToString())
Response.Conten tType = "applicatio n/octet-stream"
Response.Binary Write(byteArray )

Catch ex As Exception

End Try
Aug 23 '05 #1
2 5867
You can try a sw.Flush() before you call msw.ToArray(), because the
StreamWriter has an internal buffer. Additionally, you should replace
the string concatenation with a StringBuilder for performance reasons
(if the data has more than 100 fields)

Aug 23 '05 #2
Gambler <Ga*****@discus sions.microsoft .com> wrote:
I'm trying to download a memorystream to a .csv file on the users desktop.
When I go to write out the memorystream through the byte array it seems to
truncate at 1024 and I don't know how to increase the size. Here is my code
if that helps.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

You should also consider writing to the StreamWriter every time you are
currently appending to strBuffer - there's no need for strBuffer, and
it's very inefficient to keep creating a new string each time.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Aug 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
52258
by: Hardy Wang | last post by:
Hi all: The Stream object from WebRequest.GetResponseStream() is non-seekable. How can I convert this stream to a byte array? For ordinary Stream (seekable), I can use StreamObject.Read(myByteArray, 0, myLength) --
3
9505
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
2
36543
by: Dunc | last post by:
Hi, I've got an HttpWebResponse object, all working fine. I'm getting the underlying stream and currently writing it to a file using the ReadToEnd() method. All happy. I want to do a bit of manipulation before writing this to the disk; can anyone point me in the right direction of how to copy it to a byte array so I can make my mods before writing it to the disk?
0
3596
by: CroDude | last post by:
Hi all! I have problems when writting bitmap to a byte array and after reading it back form byte to Bitmap object. What I do is this: First I throw Bitmap to a memory-stream and then I write it into byte from a stream. Exception (System.ArgumentException: Invalid parameter used) occurs when reading from byte over a memory-stream back to the Bitmap object. Please help, I'm really stuck here! Here's the code I use (Sorry for a long...
6
2766
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
2
365
by: Gambler | last post by:
Hi, I'm trying to download a memorystream to a .csv file on the users desktop. When I go to write out the memorystream through the byte array it seems to truncate at 1024 and I don't know how to increase the size. Here is my code if that helps. Thanks. Dim msw As MemoryStream = New MemoryStream msw.SetLength(5000)
3
2608
by: Marc Gravell | last post by:
It might just be my tired eyes, but I can't see what is wrong in the following: I have a byte array filled with random data (with a fixed seed to make reproducable); I then compress and decompress this using the 2.0 GZip compression classes - however, as you see, the last byte is getting dropped. Any ideas? Almost certainly me having a blond moment ;-( Marc
1
4832
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of algorithm, initialization vector (IV), mode, padding, key etc, but I just don't get the two languages to "understand each other", or, in other words, I must be missing out on something crucial. I encrypt a byte array in C# and send over the byte...
2
17958
by: MimiMi | last post by:
I'm trying to decrypt a byte array in java that was encrypted in C#. I don't get any error messages, just a result that's completely not what I was hoping for. I think I am using the same type of algorithm, initialization vector (IV), mode, padding, key etc, but I just don't get the two languages to "understand each other", or, in other words, I must be missing out on something crucial. I encrypt a byte array in C# and send over the byte...
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.