Alo*_*lok -1 .net c# sql asp.net session
只是想学习asp.net我能够登录并重定向到default.aspx.我试图检查用户的usere名称和密码是否正确来自数据库(能够做到).现在我想在会话中存储用户名(无法做到,这里我得到nullpointer/value错误)并在下一页/重定向页面上将此会话值显示为欢迎消息.我的代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
SqlDataReader dr;
SqlConnection con = new SqlConnection("Data Source=xxxx-pc\\ddd;Initial Catalog=db_Login;User Id=sd;Password=ds;");
con.Open();
SqlCommand com = new SqlCommand("select password from tab_userinformation where Username='" + txt_uname.Text + "'", con);
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
if (dr[0].ToString() == txt_pwd.Text)
{
Response.Redirect("default_Page.aspx");
//Response.Redirect("erp.htm");
Session["user"] = txt_uname.Text;
Run Code Online (Sandbox Code Playgroud)
这里我得到的对象引用没有设置为Session ["user"]的对象异常的实例
任何建议谢谢!
因为在为会话分配值之前,您将重定向到另一个页面.
Response.Redirect("default_Page.aspx");
//Response.Redirect("erp.htm");
Session["user"] = txt_uname.Text; // this is not executing...
Run Code Online (Sandbox Code Playgroud)
您必须在重定向到页面之前指定值.例如
Session["user"] = txt_uname.Text; // this is not executing...
Response.Redirect("default_Page.aspx");
//Response.Redirect("erp.htm");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2041 次 |
| 最近记录: |