Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:49:44+00:00 2026-06-07T05:49:44+00:00

I am writing code to store cookies for Remember Me next time in Login

  • 0

I am writing code to store cookies for Remember Me next time in Login control. Which this will store the user’s username on the textbox if the user had checked the “Remember Me” check box. And my login control was set inside LoginView.

When i run the program and fill in the fields in the login control and hit submit(with or without checking the check box), it gave me this error:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 65: 
Line 66:         HttpCookie myCookie = new HttpCookie("myCookie");
Line 67:         if (RememberMe.Checked == true) //here is the line giving error
Line 68:         {
Line 69:             myCookie.Values.Add("username", Login1.UserName);

Line: 67 

This is the code for login control:

 <asp:LoginView ID="LoginView1" runat="server">
        <AnonymousTemplate> 
        <asp:Login ID="Login1" runat="server" onloggingin="Login1_LoggingIn" 
                onloginerror="Login1_LoginError" onauthenticate="Login1_Authenticate" 
                RememberMeSet="True">
            <LayoutTemplate>
                <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
                    <tr>
                        <td>
                            <table cellpadding="0">
                                <tr>
                                    <td align="center" colspan="2">
                                        Log In</td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                            ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                            ToolTip="User Name is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
                                            ControlToValidate="Password" ErrorMessage="Password is required." 
                                            ToolTip="Password is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <asp:CheckBox ID="RememberMe" runat="server" 
                                            Text="Remember me next time." />
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2" style="color:Red;">
                                        <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right" colspan="2">
                                        <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                                            ValidationGroup="ctl00$Login1" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:Login>
      </AnonymousTemplate>

      <LoggedInTemplate> 
          <asp:LoginStatus ID="LoginStatus1" runat="server" />

      </LoggedInTemplate>
        </asp:LoginView>

This is the code at the back:

protected void Page_Load(object sender, EventArgs e)
{
    System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
    TextBox UserName = (TextBox)Login1.FindControl("UserName");


    if (Request.Cookies["myCookie"] != null)
    {

        HttpCookie cookie = Request.Cookies.Get("myCookie");
        Login1.UserName = cookie.Values["username"];

        //.Attributes.Add("value", cookie.Values["password"]);
        Response.Cookies["myCookie"].Expires = DateTime.Now.AddDays(-1);


    }
} 

protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
     System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
    TextBox UserName = (TextBox)Login1.FindControl("UserName");

    //Check to see if the current user exists
    if (Membership.GetUser(Login1.UserName) != null)
    {
        //Check to see if the user is currently locked out
        if (Membership.GetUser(Login1.UserName).IsLockedOut)
        {
            //Get the last lockout  date from the user
            DateTime lastLockout = Membership.GetUser(Login1.UserName).LastLockoutDate;
            Response.Write(lastLockout.ToString()); 
            //Calculate the time the user should be unlocked
            DateTime unlockDate = lastLockout.AddMinutes(Membership.PasswordAttemptWindow);

            //Check to see if it is time to unlock the user
            if (DateTime.Now > unlockDate)
                Membership.GetUser(Login1.UserName).UnlockUser();
        }
    }


}


protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
    TextBox UserName = (TextBox)Login1.FindControl("UserName");

    Response.Cookies.Add(new HttpCookie("UserName", Login1.UserName));
    CheckBox RememberMe =  LoginView1.FindControl("RememberMe") as CheckBox;
    //CheckBox RememberMe = (CheckBox).Login1.FindControl("RememberMe"); 

    HttpCookie myCookie = new HttpCookie("myCookie");
    if (RememberMe.Checked == true)
    {
        myCookie.Values.Add("username", Login1.UserName);
        myCookie.Expires = DateTime.Now.AddDays(15);
        Response.Cookies.Add(myCookie);
    }

}


protected void Login1_LoginError(object sender, EventArgs e)
{
    System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
    TextBox UserName = (TextBox)Login1.FindControl("UserName");
    Literal FailureText = (Literal)Login1.FindControl("FailureText");


    //There was a problem logging in the user
    //See if this user exists in the database

    MembershipUser userInfo = Membership.GetUser(Login1.UserName);

    if (userInfo == null)
    {
        //The user entered an invalid username...

        Login1.FailureText = "There is no user in the database with the username " + UserName.Text;
    }
    else
    {
        //See if the user is locked out or not approved
        if (!userInfo.IsApproved)

            Login1.FailureText = "When you created your account you were sent an email with steps to verify your account. You must follow these steps before you can log into the site.";

        else if (userInfo.IsLockedOut)

            Login1.FailureText = "Your account has been temporary locked due to a maximum number of incorrect login attempts.";

        else

            //The password was incorrect (don't show anything, the Login control already describes the problem)
            Login1.FailureText = string.Empty;

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:49:45+00:00Added an answer on June 7, 2026 at 5:49 am

    In your code-behind file, try replacing the Login1_Authenticate method with the following code:

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        System.Web.UI.WebControls.Login Login1 = (System.Web.UI.WebControls.Login)LoginView1.FindControl("Login1");
        TextBox UserName = (TextBox)Login1.FindControl("UserName");
    
        Response.Cookies.Add(new HttpCookie("UserName", Login1.UserName));
    
        // IMPORTANT: Notice that LoginView1 is changed to Login1
        CheckBox RememberMe =  Login1.FindControl("RememberMe") as CheckBox; 
    
        HttpCookie myCookie = new HttpCookie("myCookie");
        if (RememberMe.Checked == true)
        {
            myCookie.Values.Add("username", Login1.UserName);
            myCookie.Expires = DateTime.Now.AddDays(15);
            Response.Cookies.Add(myCookie);
        }
    
    }
    

    I think the problem you are having is related to the fact that the FindControl method doesn’t search hierarchically – that is, it only will find a child control that is directly contained within the parent. It doesn’t search through the layers.

    In your case, the check box is contained within Login1, which in turn is a child of LoginView1. So you need to search within Login1, and not LoginView1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some code (just for fun so far) in Python that will store
I am writing a code, wherein I will store few files into the data-structure
i m writing this code in my code to store the data in database..
I'm writing a application where the user can write json-code and store that json
I'm currently writing some code which will download images from my web server for
I am writing code that will search twitter for key words and store them
When writing C++ code I've learned that using the stack to store memory is
I am writing code that will populate the Margin , Padding and BorderThickness properties
I am writing the following code on boost's program_options (version 1.42). This seems straight-forward
I am writing code that compares 2 bytes which represent integers. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.