1
votes

On load of ASP.Net page, a report function is called.

protected void Page_Load(object sender, EventArgs e)
{
    GlobalFunctions obj = new GlobalFunctions();
    obj.GetReport(Page PageName, string ReportName);
}

GetReport is defined as :

public void GetReport(Page PageName, string ReportName)
{
    ReportClass rpt = new ReportClass();
    rpt = GetReportFromDLL(ReportName);   //No error here
    rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, ReportName); 
}

Error :

"Response" is not accessible through the Class.

I have tried using "HttpContext.Current.Response" in place of "Response"

rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat,HttpContext.Current.Response, true, ReportName);

But I get this error:

"Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."

Please help!

2
have you tried using a reference instead of an instance? I think Page has issues with copying. - Camron
I suspect passing Response as parameter is not acceptable due to coding style, but consider to try it anyway... - Alexei Levenkov
@Camron - What should i pass as reference? Please clarify - Ish Goel
@AlexeiLevenkov - The code works if GetReport function is in the Default.aspx page. Currently, since the GetReport function resides in GlobalFunctions.cs, "Response" is not accessible. - Ish Goel
sorry for not clarifying, I'm new to giving advice. I think the page variable. If I remember correctly the Page instance is created outside of the scope of the implementation itself, and in fact outside of the instance that is the server response. I think that the Page instance is created by the thread that spawns the IIS responses, and is protected from copy. - Camron

2 Answers

0
votes

I'm not sure why you can't pass response to a function... Try following

  public void GetReport(HttpResponse response, string ReportName)...

And call as:

   obj.GetReport(Response, "some report name");
0
votes

After quite an effort, I found that ExportToHttpReponse always threw an error "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." if the entire code block is NOT in a Try-Catch Block.

Here is the solution to avoid this error:

public void GetReport(Page PageName, string ReportName)
{
    try
    {
        ReportClass rpt = new ReportClass();
        rpt = GetReportFromDLL(ReportName);   //No error here
        rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, ReportName);
    }
    catch (Exception ex)
    {
        //do nothing
    } 
}

This just removes the error and the report gets printed on the HttpResponse.