jlrolin
jlrolin

Reputation:

Response.Write DataTable Data to Text File, ASP.net Hangs

Very odd problem as this is working perfectly on our old Classic ASP site. We are basically querying the database and exporting around 2200 lines of text to a Text File through Response.Write to be output to a dialog box and allows the user to save the file.

Response.Clear() Response.ClearContent() Response.ClearHeaders()

    Dim fileName As String = "TECH" & test & ".txt"

    Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", fileName))
    Response.ContentType = "text/plain"

    Response.Write(strHeader)

    Dim sw As New IO.StringWriter()

    Dim dtRow As DataRow
    For Each dtRow In dt3.Rows
        sw.Write(dtRow.Item("RECORD") & vbCrLf)
    Next

    Response.Write(sw.ToString)
    Response.Write(strTrailer & intRecCount)
    Response.End()

I can either use StringWriter or simply use Response.Write(dt.Rows(i).Item("RECORD").toString

Either way, the Export is causing a horrendous hang on our development site. My local machine causes no hang and is almost instantaneous. The recordset isn't very large, and the lines it is writing are small.

Anyone have any idea why this would be hanging? It does EVENTUALLY allow for a save and display the file, but it's well over 3-4 minutes.

Upvotes: 2

Views: 11208

Answers (3)

Sunny Milenov
Sunny Milenov

Reputation: 22320

Both using StringWriter and DataTable are overkill.

Why not use directly SqlReader to get the results from the database, and while reading the reader, write directly to the output stream? Much faster, and much less memory consumed.

As an answer to your second question - why the ASP was working OK, I doubt that there you have stored the same content 3 times in memory in order to output it (in the DataTable, in the StringWriter and in the output buffer). My ASP is a little bit rusty, but I would guess that there you are using database reader of some sort.

Also, better employ some logging infrastructure (NLog, log4net), so you can output some timing about which operation delays how much, as an alternative to attaching a remote debugger.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 416081

Sounds like maybe you're overflowing the output buffer. Perhaps add a counter in there to flush every few hundred lines.

Also, the Response object basically does most of the work for a StringWriter for you. Using the StringWriter as an intermediary is probably redundant.

Upvotes: 0

FlySwat
FlySwat

Reputation: 175693

Attach a remote debugger and find where its hanging?

You need to figure out if its the string writer loop, or the actual query code (which is not provided here).

Upvotes: 2

Related Questions