登录跳转页面,跳转后得不到session的值

wxxloveu 2010-04-16 02:20:27
登录页面是这样的,

protected void btn_ok_Click(object sender, EventArgs e)
{
OleDbConnection con = new ConnectAccess().getACon();
try
{
string sql = "select * from userInfo where UserName='"+this.txt_UserName.Text.ToString().Trim()+"'";

OleDbCommand cmd = new OleDbCommand(sql,con);

cmd.Connection = con;
cmd.CommandType = CommandType.Text;
con.Open();

OleDbDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
this.txt_UserName.Text = "ok";
//string pwd = reader["password"].ToString();
if (reader["password"].ToString().Trim() == this.txt_pwd.Text.Trim())
{
string sid = reader["ID"].ToString();//sid的值为数据库中的值
string susername = reader["UserName"].ToString();//id也为数据库中对应的id,正确

Session["ID"] = sid;
Session["UserName"] = susername;

susername = Session["UserName"].ToString();//这里susername的值是上面的值,说明这里session赋值成功
Response.Redirect("Default.aspx");
}
else
{
this.lblWrongUser.Text = "Your password is wrong";
this.lblWrongUser.Visible = true;
}
}
else
{
this.lblWrongUser.Visible = true;
//this.txt_UserName.Text = "no that guy";
}
con.Close();
}
catch (Exception ex)
{
con.Close();
this.txt_UserName.Text = "false";
this.lblWrongUser.Text = "There maybe something wrong with the database,"+ex.ToString();
this.lblWrongUser.Visible = true;
}
}

跳转页面的代码:

protected void Page_Load(object sender, EventArgs e)
{
try
{
if (Session["UserName"] != null)//session 的值总是为null???
{
string userName = Session["UserName"].ToString();
this.lbl_Welcome.Text = "欢迎" + userName + "再次来到cincin的家园";
}
else
{
Response.Write("Session[\"UserName\"] is null");//跳转后一直显示此状态,即session为null
}
//string id = Session["ID"].ToString();
}
catch(Exception ex)
{
Response.Write("跳转页面时出错\n" + ex.ToString());
}
}
...全文
581 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxxloveu 2010-04-16
  • 打赏
  • 举报
回复
以后还望各位多多指教啊……
wxxloveu 2010-04-16
  • 打赏
  • 举报
回复
谢谢各位,特别是4楼,问题解决了:
原因:

bin目录中的文件被改写,asp.net有一种机制,为了保证dll重新编译之后,系统正常运行,它会重新启动一次网站进程,这时就会导致 Session丢失,所以如果有access数据库位于bin目录,或者有其他文件被系统改写,就会导致Session丢失

解决办法:
把数据库文件换了个文件夹……
wxxloveu 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 fwacky 的回复:]
---修改的代码 试试
OleDbConnection con = new ConnectAccess().getACon();
try
{
string sql = "select * from userInfo where UserName='" + this.txt_UserName.Text.ToStri……
[/Quote]
貌似还是不行。改了这样的:

string sql = "select * from userInfo where UserName='"+this.txt_UserName.Text.ToString().Trim()+"'";

OleDbCommand cmd = new OleDbCommand(sql,con);

cmd.Connection = con;
cmd.CommandType = CommandType.Text;
con.Open();

OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;

cmd.ExecuteScalar();

DataSet ds = new DataSet();
da.Fill(ds);
da.Dispose();
fwacky 2010-04-16
  • 打赏
  • 举报
回复


---修改的代码 试试
OleDbConnection con = new ConnectAccess().getACon();
try
{
string sql = "select * from userInfo where UserName='" + this.txt_UserName.Text.ToString().Trim() + "'";

OleDbCommand cmd = new OleDbCommand(sql, con);

cmd.Connection = con;
cmd.CommandType = CommandType.Text;
con.Open();
OleDbDataAdapter oleApter = new OleDbDataAdapter();
DataSet ds = new DataSet();
oleApter.Fill(ds);
oleApter.Dispose();


if (ds != null && ds.Tables[0].Rows.Count > 0)
{

this.txt_UserName.Text = "ok";
//string pwd = reader["password"].ToString();
if (ds.Tables[0].Rows[0]["password"].ToString().Trim() == this.txt_pwd.Text.Trim())
{
string sid = ds.Tables[0].Rows[0]["ID"].ToString();//sid的值为数据库中的值
string susername = ds.Tables[0].Rows[0]["UserName"].ToString();//id也为数据库中对应的id,正确

Session["ID"] = sid;
Session["UserName"] = susername;

susername = Session["UserName"].ToString();//这里susername的值是上面的值,说明这里session赋值成功
Response.Redirect("Default.aspx");
}
else
{
this.lblWrongUser.Text = "Your password is wrong";
this.lblWrongUser.Visible = true;
}
}
else
{
this.lblWrongUser.Visible = true;
//this.txt_UserName.Text = "no that guy";
}

}
catch (Exception ex)
{

this.txt_UserName.Text = "false";
this.lblWrongUser.Text = "There maybe something wrong with the database," + ex.ToString();
this.lblWrongUser.Visible = true;
}
finally
{
cmd.Dispose();
con.Close();
}

wxxloveu 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 qqhahazxxqq 的回复:]
用Response.Redirect("Default.aspx");
跳转回造成 Session丢失
原因:当asp.net 执行 Response.Redirect 时会强制终止当前Response ,不发送当前页面的cookie 给浏览器,而是发送一个指令告诉浏览器重新发送一个新的HTTP请求到新的URL,结果导致当前的Session 丢失。
[/Quote]
好的,谢谢,但是我用你的还是一样的效果
qqhahazxxqq 2010-04-16
  • 打赏
  • 举报
回复
用Response.Redirect("Default.aspx");
跳转回造成 Session丢失
原因:当asp.net 执行 Response.Redirect 时会强制终止当前Response ,不发送当前页面的cookie 给浏览器,而是发送一个指令告诉浏览器重新发送一个新的HTTP请求到新的URL,结果导致当前的Session 丢失。
wxxloveu 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 pengyi_205 的回复:]

禁用了cookie没有?
OleDbDataReader reader = cmd.ExecuteReader();
好像没关闭 reader.Close()
[/Quote]
谢谢提醒,IED的cookie设置为最低的了
[Quote=引用 16 楼 luoweihua7 的回复:]

仔细看了一下.应该不会有问题的.
你试试干掉try看看.好多代码可以不写的
string sql代码里面很容易被注入啊.
还有就是select的时候干嘛不把密码一起select了?

[/Quote]
谢谢回复,毕业后就没有做过web了,练练手
fwacky 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wxxloveu 的回复:]
引用 12 楼 echo0808 的回复:
你确定你session拿到值了?

确定啊
[/Quote]
if(reader.Read())

循环了多次! 把第一次的覆盖了!
你看看,是不是查出多个来!
V-Far 2010-04-16
  • 打赏
  • 举报
回复
仔细看了一下.应该不会有问题的.
你试试干掉try看看.好多代码可以不写的
string sql代码里面很容易被注入啊.
还有就是select的时候干嘛不把密码一起select了?

[Quote=引用 5 楼 lyvscf 的回复:]

Session["ID"] = sid;
Session["UserName"] = susername;

susername = Session["UserName"].ToString();//
这里怎么这样做 不解?
[/Quote]

这个应该是lz想检查一下session["username"]是否有值吧
wxxloveu 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 echo0808 的回复:]
你确定你session拿到值了?
[/Quote]
确定啊
夺命胖子 2010-04-16
  • 打赏
  • 举报
回复
禁用了cookie没有?


OleDbDataReader reader = cmd.ExecuteReader();


好像没关闭 reader.Close()
zxj811210 2010-04-16
  • 打赏
  • 举报
回复
你把Session["UserName"] != null改成
Session["UserName"].ToString() != ""试试
echo0808 2010-04-16
  • 打赏
  • 举报
回复
你确定你session拿到值了?
qqhahazxxqq 2010-04-16
  • 打赏
  • 举报
回复
把这句
Response.Redirect("Default.aspx");
换成

Response.Write("<script>window.location.href='Default.aspx';</script>");
cnl432 2010-04-16
  • 打赏
  • 举报
回复
小心别人攻击你,注入攻击
fwacky 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lyvscf 的回复:]
Session["ID"] = sid;
Session["UserName"] = susername;

susername = Session["UserName"].ToString();//
这里怎么这样做 不解?
[/Quote]

楼上,那是 楼主的测试!
wx8849 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lyvscf 的回复:]
Session["ID"] = sid;
Session["UserName"] = susername;

susername = Session["UserName"].ToString();//
这里怎么这样做 不解?
[/Quote]

他这样只是测试session里面有没有值
wxxloveu 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lyvscf 的回复:]

Session["ID"] = sid;
Session["UserName"] = susername;

susername = Session["UserName"].ToString();//
这里怎么这样做 不解?
[/Quote]
没什么,为了调试方便,看看能不能用session赋值,结果是可以赋值
lyvscf 2010-04-16
  • 打赏
  • 举报
回复
哦 你在调试
lyvscf 2010-04-16
  • 打赏
  • 举报
回复
Session["ID"] = sid;
Session["UserName"] = susername;

susername = Session["UserName"].ToString();//
这里怎么这样做 不解?
加载更多回复(4)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧