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

x
  • 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;
        context.Response.Clear();

        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.Write(Environment.NewLine);
        context.Response.ContentType = "text/csv";
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=membSel.csv");
        context.Response.End();
    }

0
national_fulfillment
Asked:
national_fulfillment
1 Solution
 
smulangeCommented:
I think you should use sql command and set the time out to it

1.

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
0
 
jagrut_patelCommented:
Check whether setting executionTimeout attribute of httpRuntime element in web.config is useful. See this for more details about httpRuntime element.
0
 
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.
0

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