So i would like the user to login and have their username appear on the the top of the page when they're logged in. I check if they exist in the database first then i let them in, so when they click the login button it does something like this(button event)
所以我希望用户登录并在登录时将他们的用户名显示在页面顶部。我先检查它们是否存在于数据库中,然后我让他们进入,所以当他们点击登录按钮时做这样的事情(按钮事件)
If (IsPostBack) Then
connection.Open()
command.Connection = connection
command.CommandText = "select count(*) from customer where Email='" & TextEmail.Text & "' AND Password='" & TextPassword.Text.Trim & "'"
counts = Convert.ToInt32(command.ExecuteScalar.ToString) ' execute this text as an SQL query
If (counts > 0) Then
Response.Redirect("Default.aspx", False)
Session("Username") = TextEmail.Text 'This is where i try to use the session storing the user's email (which i use as a username)
Else
Label1.Visible = True
End If
connection.Close()
End If
Above code works fine, has no problem. In C#.Net This would be Session["Username"]=TextEmail.Text right.
上面的代码工作正常,没有问题。在C#.Net中这将是Session [“Username”] = TextEmail.Text。
So now there is a form within my project to which you cannot gain access without logging in first. When this page loads, i want to check if the user is logged in or not, if not, I redirect them to the Login page, if they are i let them in, and update the label that will display "logged in user userEmail", The Page_load code is like this:
所以现在我的项目中有一个表单,如果没有先登录,您将无法访问该表单。当这个页面加载时,我想检查用户是否登录,如果没有,我将它们重定向到登录页面,如果他们让我们进入,并更新将显示“登录用户userEmail”的标签,Page_load代码是这样的:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not (Session("Username") = vbNull) Then
LogInStatus.Text = "logged in user " & System.Web.HttpContext.Current.Session("Username").ToString
Else
Response.Redirect("Login.aspx", False)
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
There will be a Logout, which when clicked i will set the Session to Null and then redirect them to the login page once again. so when they click logout this happens
将有一个Logout,当单击时,我将Session设置为Null,然后再次将它们重定向到登录页面。所以当他们点击退出时会发生这种情况
Session("Username") = vbNull
Response.Redirect("Login.aspx", False)
When i run the code, i get this NullReference exception image here Obviously either i'm initializing the object wrong, or i'm just using it straight the wrong way. But all this works with C#. Please help.
当我运行代码时,我在这里得到这个NullReference异常图像显然,我正在初始化对象错误,或者我只是以错误的方式使用它。但这一切都适用于C#。请帮忙。
1 个解决方案
#1
0
The problem is: when "Username" key doesn't exists, the value you get from Session is the object Nothing, not vbNull. Then Nothing ToString raises an exception. Try this:
问题是:当“用户名”键不存在时,从Session获得的值是对象Nothing,而不是vbNull。然后Nothing ToString引发异常。尝试这个:
If Session("Username") IsNot Nothing Then
LogInStatus.Text = "logged in user " & Session("Username").ToString
Else
Response.Redirect("Login.aspx", False)
End If
Rather than assigning vbNull you may do Session.Remove("Username") which sets the value to Nothing.
您可以使用Session.Remove(“Username”)将值设置为Nothing,而不是分配vbNull。
#1
0
The problem is: when "Username" key doesn't exists, the value you get from Session is the object Nothing, not vbNull. Then Nothing ToString raises an exception. Try this:
问题是:当“用户名”键不存在时,从Session获得的值是对象Nothing,而不是vbNull。然后Nothing ToString引发异常。尝试这个:
If Session("Username") IsNot Nothing Then
LogInStatus.Text = "logged in user " & Session("Username").ToString
Else
Response.Redirect("Login.aspx", False)
End If
Rather than assigning vbNull you may do Session.Remove("Username") which sets the value to Nothing.
您可以使用Session.Remove(“Username”)将值设置为Nothing,而不是分配vbNull。