Cookies Not Being Added
On the login page I made, this is开发者_开发知识库 part of the code behind for the submit button.
Dim aCookie As New HttpCookie("userInfo")
aCookie.Values("user") = Me.usr.Text
aCookie.Values("last") = Now.ToString()
If Me.remember.Checked() Then
aCookie.Expires = DateTime.Now.AddDays(3650)
Else
aCookie.Expires = DateTime.Now.AddHours(1)
End If
Response.Cookies.Add(aCookie)
'Response.Redirect("~/Default.aspx")
testBox.Text = Response.Cookies("userInfo")("user").ToString()
For some reason, when it gets to the last line, it throws an error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
On your first line you call the cookie userInfo
and on the last line you are referencing a cookie named loginInfo
, assuming you are trying to access the cookie you just created that's your problem.
Try
testBox.Text = Response.Cookies("userInfo")("user").ToString()
精彩评论