程序代码如下:
try
{
conn.Open();
string str_sql = "select * from tb_User where UName='" + txtUName.Text.Trim() + "' and UPwd='" + txtUPwd.Text.Trim() + "'";
SqlCommand comm = new SqlCommand(str_sql, conn);//创建sqlcommand对象
reader = comm.ExecuteReader(); //创建sqldatareader对象,并赋值
//如果数据库中有相应记录
if (reader.Read())
{
Session["yonghuming"] = txtUName.Text.Trim(); //将用户名存在session变量中
Session["mima"] = txtUPwd.Text.Trim();
//Response.Write("<script>alert('" + Session["mima"].ToString() + "')</script>");
conn.Dispose();
conn.Close();
Response.Redirect("index.aspx");
}
//如果没有,则提示错误消息
else
{
Response.Write("<script>alert('用户名或密码错误,请重新输入!')</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
finally
{
conn.Disponse();
conn.Close();
}
这段程序在运行没有提示错误,只是在我一步步调试时才发现了这样一个错误,用户名和密码全部正确后也登录到index.aspx页面了,可就是提示错误。只要把response.redirect()放到try语句外面即可。