C# time out exception when passing data to http response

Posted on 2011-10-05
Last Modified: 2012-06-21
I"m trying to send the user a pretty decent about of data could be about 200,000 records or more. WHen i run the selections it always times out. If i run smaller ones it works. Heres the code to export: I'm i goign about this wrong, is there a better way to achieve what i want in terms of performance. I need to get the user a csv file of data from a stored procedure.

public void ExcelExportCSV(SqlDataReader dr )
        HttpResponse response = HttpContext.Current.Response;
        HttpContext context = HttpContext.Current;

        while (dr.Read())
            context.Response.Write(dr.GetValue(0)+ ",");
            context.Response.Write(dr.GetValue(1) + ",");
            context.Response.Write(dr.GetValue(2) + ",");
            context.Response.Write(dr.GetValue(3) + ",");
            context.Response.Write(dr.GetValue(4) + ",");
            context.Response.Write(dr.GetValue(5) + ",");
        context.Response.ContentType = "text/csv";
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=membSel.csv");

Question by:national_fulfillment
    LVL 1

    Accepted Solution

    I think you should use sql command and set the time out to it


    SqlCommand cmd = new SqlCommand("sql statement", connection);
    cmd.CommandTimeout = 999999 ;
    cmd.CommandType    = CommandType.Text;

    // open connection;
    SqlDataReader dr = cmd.ExecuteReader();

    then add logic to read records

    while (dr.Read())

    2. apart from this you need to add timeout configuration in web.config inside <system.web>  to avoid HtttpRequest timeout exception

    ex: <httpRuntime maxRequestLength="100000" executionTimeout="36000" />

    hope this will resolve issue
    LVL 8

    Expert Comment

    Check whether setting executionTimeout attribute of httpRuntime element in web.config is useful. See this for more details about httpRuntime element.

    Author Closing Comment

    i had a couple other issues but i think adding the timeout on the sql command did the trick. Thanks a bunch.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now