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

实例代码:

         try
        
{
            connection 
= new OleDbConnection(connectionString);

            connection.Open();
            OleDbCommand cmd 
= connection.CreateCommand();
            cmd.CommandText 
= "select * from UserInfo";
            
/*            OleDbDataReader reader = cmd.ExecuteReader();*/
            a 
= new OleDbDataAdapter(cmd);
            
//
            DataSet d = new DataSet();
            a.Fill(d);
            connection.Close();
            d.Tables[
0].DefaultView.RowFilter = "userId =" + "'" + strManager + "'";
            
if (d.Tables[0].DefaultView.Count > 0)
            
{
                HttpCookie cookie 
= new HttpCookie("txtManager", txtManager.Text);
                Response.Cookies.Add(cookie);
                cookie 
= new HttpCookie("txtPassword", txtPassword.Text);
                Response.Cookies.Add(cookie);
                Response.Redirect(
"UserInfo.aspx");
            }

            
else
            
{
                Response.Redirect(
"Error.aspx");
            }


        }

        
catch  (System.Exception ex)
        
{
           Response.Redirect(
"Error.aspx");
        }

 

如果你运行上面类似的代码,你会发现无论怎么走,都回走到异常处理,冲定向到Error页面。

问题原因及解决办法:

原因
Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件。 Response.End 后面的代码行将不执行。

此问题出现在 Response.Redirect 和 Server.Transfer 方法中,这是由于这两种方法都在内部调用 Response.End。
解决方案
若要解决此问题,请使用下列方法之一:
对于 Response.End,调用 ApplicationInstance.CompleteRequest 方法而不调用 Response.End,以便跳过 Application_EndRequest 事件的代码执行。
对于 Response.Redirect,使用重载 Response.Redirect(String url, bool endResponse),对 endResponse 参数它传递 false以取消对 Response.End 的内部调用。例如:
   Response.Redirect ("nextpage.aspx", false);
如果使用这种解决方法,Response.Redirect 后面的代码将得到执行。
对于 Server.Transfer,请改用 Server.Execute 方法。
状态
这种现象是设计使然。
解决后的代码:
try
{
Response.Redirect("UserManager",false);
}
catch
{
Response.Redirect("Error.aspx");
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值