Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I need to download csv file from asp.net with vb.net
i have done the download csv file from vb.net
The moment i raise you that, when i download less than 50 rows then the file download very fast but when i download more than 100 rows then it has come very slow (means come around after 5 minutes because data is very huge)
So what is the essential step i need to handle for this issue resolved.

Please find my code below,

Using dt As New DataTable()
                  Adp.Fill(dt)

                  'Build the CSV file data as a Comma separated string.
                  Dim csv As String = String.Empty

                  For Each column As DataColumn In dt.Columns
                      'Add the Header row for CSV file.
                      csv += column.ColumnName + ","c

                  Next

                  'Add new line.
                  csv += vbCr & vbLf

                  For Each row As DataRow In dt.Rows
                      For Each column As DataColumn In dt.Columns
                          'Add the Data rows.
                          csv += row(column.ColumnName).ToString().Replace(",", ";") + ","c
                      Next

                      'Add new line.
                      csv += vbCr & vbLf
                  Next

                  'Download the CSV file.
                  Response.Clear()
                  Response.Buffer = True
                  Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv")
                  Response.Charset = ""
                  Response.ContentType = "application/text"
                  Response.Output.Write(csv)
                  Response.Flush()
                  Response.End()
              End Using


thanks in advance.


regards,
stellus.

What I have tried:

csv download getting very slow in vb.net
Posted
Updated 16-Sep-16 23:57pm
Comments
Ralf Meier 17-Sep-16 13:08pm    
I can't see a generell mistake in your code - sorry. I think, that your problem is the "getting of the data" itself and not your code-construction. Of course : the code could be optimized - but that doesn't solve your "download-Problem" ...

 
Share this answer
 
Comments
Karthik_Mahalingam 17-Sep-16 3:49am    
5
Mehdi Gholam 17-Sep-16 3:50am    
Cheers !
A little trick to speedup your code
VB
Dim csv As String = String.Empty
Dim row_csv As String

...

For Each row As DataRow In dt.Rows
    row_csv = String.Empty
    For Each column As DataColumn In dt.Columns
        'Add the Data rows.
        row_csv += row(column.ColumnName).ToString().Replace(",", ";") + ","c
    Next

    'Add new line.
    row_csv += vbCr & vbLf
    csv += row_csv
Next
 
Share this answer
 
Comments
Karthik_Mahalingam 18-Sep-16 2:13am    
Good idea, but however StringBuilder() has much better performance,
i have faced it many times
Patrice T 18-Sep-16 4:24am    
I know, it is the kind of optimization that was used when string builder did not exist.
Karthik_Mahalingam 18-Sep-16 4:45am    
hmm, good to know.
Patrice T 18-Sep-16 5:30am    
Repeated addition of a little string to another one involve a workload which grow with the square of the number of operations.
For n operations, the workload is n*(n+1)/2 or (n^2+n)/2.
1 + 2 + 3 + 4 + ⋯ - Wikipedia, the free encyclopedia[^]
In this case say that you have 10 rows and 10 columns.
OP solution gives n=100 with operation size of 1, it gives a workload of 100*101/2=5050
with my solution, building of a row is n=10, size=1 and workload=55
building of the big string is n=10, size=10 (because each row is 10 cells), workload=550
Global workload is 55*10+550=1100
With 100 rows, the gain is even better.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900