日期:2014-05-17  浏览次数:20432 次

seesion信息不能连接到下一页面
本人是初学者,可能会犯一些低级错误的
我从登陆页面可以将session信息带到登陆成功后的页面,但从注册页面却不能携带sessio过去,下面是注册页面的cs部分代码
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string zName = ztxname.Text;
        string aPassword = upw2.Text;
        string aAge = txuserage.Text;
        string aSex = rasex.Text;
        string aQQ = txuserQQ.Text;
        string aAdd = txadd.Text;
        string aRole = radrole.Text;
        string aMail = txemail.Text;
        string conStr = "Data Source=RONEYSAM;Initial Catalog=ASPNETDB09;Persist Security Info=True;User ID=winco;Password=Tobelove920207";
        SqlConnection conn = new SqlConnection(conStr);
                conn.Open();
        string sqlStr=string.Format("insert into tbUser(userName,userPassword,usereAge,userSex,userQQ,userAddress,userRole,userEmail)"
            +" values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');",zName,aPassword,aAge,aSex,aQQ,aAdd,aRole,aMail);
        SqlCommand cmd= new SqlCommand(sqlStr,conn);
        if (cmd.ExecuteNonQuery() > 0)
        {
            Session["UName"] = zName;
            Response.Write("<script>alert('注册成功!');window.location='Success.aspx'</script>");
        }
        else
        {
            Response.Write("<script>alert('注册失败!');</script>");
        }
        conn.Close();
    }

(还有就是不能写入数据库,又出现了什么错误呢?)
登陆成功后获取session的代码如下
 protected void Page_Load(object sender, EventArgs e)
    {
        if(this.Session["UName"]!=null)
        {
            this.Label1.Text = this.Session["UName"].ToString();
        }
        else{
            Response.Redirect("Default.aspx");
        }
    }

因为session是null,所以就跳转到登陆页面了
------解决方案--------------------
你的 if (cmd.ExecuteNonQuery() > 0)这一步成功了吗
------解决方案--------------------