62,046
社区成员
发帖
与我相关
我的任务
分享
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());
}
}
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();
---修改的代码 试试
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();
}