471,071 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

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(5000)

Dim sw As New StreamWriter(msw)

' 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.Count

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

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

strBuffer += ControlChars.NewLine

' 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.NewLine
Next
sw.Write(strBuffer)

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

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

'Add Headers to enable dialog display
Response.AddHeader("Content-Disposition", "attachment;
filename=TownGrants.csv")
Response.AddHeader("Content-Length", byteArray.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.BinaryWrite(byteArray)

Catch ex As Exception

End Try
Aug 23 '05 #1
2 5503
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*****@discussions.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Aug 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dennis | last post: by
2 posts views Thread by Gambler | last post: by
3 posts views Thread by Marc Gravell | last post: by
reply views Thread by leo001 | last post: by

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.