views:

4833

answers:

6

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.

+1  A: 

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).

FlySwat
A: 

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.

Joel Coehoorn
Shouldn't Response.Write automatically flush the buffer?
FlySwat
You'd think that, but I've seen it happen. Also: reading his code he's pushing everything into response in one big operation.
Joel Coehoorn
A: 

Yes, I understand the redundancy. That code was just a simply trial and error. I'm a bit confused as to why the old ASP code works perfectly. I'm assuming our DEV environment has a problem. Attaching a remote debugger, I need some direction on how to do that.

A: 

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.

Sunny
A: 

Interestingly, my example at the top isn't exactly how my query was working. I was only using Response.Write. The StringWriter was a try at diagnosing if it happened to be a .NET vs. Classic ASP problem.

It seems that one of the queries is a problem only on our Development environment, which is even more strange. Works on my machine and Production, but not on DEV.

A: 

how if I want to display textfile with HMTL format to asp.net, I'm using C#?

please send to [email protected]. thanks :)