首页 > 代码库 > 1. Server.Transfer跳转页面抛出异常:正在终止线程

1. Server.Transfer跳转页面抛出异常:正在终止线程

今天在向数据库插入数据,之后使用Server.Transfer跳回本页面时,抛出异常:正在终止线程

ry        {            if (0 == String.Compare(Password.Text.Trim(), ConfirmPassword.Text.Trim()))//两次输入密码相同            {                MembershipUser mu = Membership.CreateUser(UserName.Text, Password.Text);//添加注册用户                Roles.AddUserToRole(UserName.Text, "worker"); //添加角色                String strConn = "server=.;database=ComInfo;integrated security=true;";//连接字符串                SqlConnection conn = new SqlConnection(strConn);//创建数据库链接                conn.Open();//打开连接                String PassUserid = Request.QueryString["C_Id"];                String userid = mu.ProviderUserKey.ToString();//获取注册ID                SqlCommand cmd = new SqlCommand("insert into Emp(E_Name,E_Sex,E_Position,E_Organisation,E_Address,E_Phone,C_Id,User_Id) values(" +                    "" + UserName.Text.Trim() + "‘," +                    "" + edt_sex.Text.Trim() + "‘," +                    "" + edt_pos.Text.Trim() + "‘," +                    "" + edt_dep.Text.Trim() + "‘," +                    "" + edt_address.Text.Trim() + "‘," +                    "" + edt_phone.Text.Trim() + "‘," +                    "" + PassUserid + "‘," +                //公司ID                    "" + userid + "" +                   //用户注册ID                    ")", conn);//sql语句,向Emp表添加数据                cmd.ExecuteNonQuery();                conn.Close();                Server.Transfer("~/User/Imformation.aspx");}            }            else                Response.Write("<script>window.alert(‘两次输入的密码不同!‘);</script>");        }        catch (Exception ex)        {            Response.Write("<script>window.confirm(‘" + ex.Message.ToString() + "‘);</script>");        }

原因:Server.Transfer的执行,将会内部调用Response.End,将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件,同时抛出ThreadAbortException 异常,异常信息为“正在中止线程”。

解决办法:将Server.Transfer语句放到finally中,不再抛出异常

        finally        {            Server.Transfer("~/User/Imformation.aspx");        }