hi,

i have a web page on which i am displaying data in gridview control from database.i want to generate the excel report of this grid data. i am using the following code behind button click. it is working well.

private void ExportGridView()
{
     string attachment = "attachment; filename=invalidproductdata.xls";
     Response.ClearContent();
     Response.AddHeader("content-disposition", attachment);
     Response.ContentType = "application/ms-excel";
     StringWriter sw = new StringWriter();
     HtmlTextWriter htw = new HtmlTextWriter(sw);
     myGridView.RenderControl(htw);
     Response.Write(sw.ToString());
     Response.End();
}

but the problem is that i want to take this code into my code class in business logic layer for reuse purpose.but when i put this method in logic layer class it creates an error. the error is " Response does not exist in the current context ".
plz help me how can i resolve this issue.

Thanks and Best Regards,
Nice Candy

Use HttpContext.Current property.

..
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
..
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.