Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

C# time out exception when passing data to http response

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");

1 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
Check whether setting executionTimeout attribute of httpRuntime element in web.config is useful. See this for more details about httpRuntime element.
national_fulfillmentAuthor Commented:
i had a couple other issues but i think adding the timeout on the sql command did the trick. Thanks a bunch.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now