[c#, session cookies]Object reference not set to an instance of an object. How to receive session cookie (added by ‘Session[“password”] = cpassword’) in good, correct way?

Last post 05-21-2007 1:57 AM by vivek_iit. 3 replies.

Sort Posts:

  • Sad [:(] [c#, session cookies]Object reference not set to an instance of an object. How to receive session cookie (added by ‘Session[“password”] = cpassword’) in good, correct way?

    05-13-2007, 9:12 AM
    • Loading...
    • szmitek
    • Joined on 03-04-2007, 12:22 AM
    • Chotomów, Poland
    • Posts 59

    I would like create page (in c# language) where I could type login, password and after checking checkbox could save "mypassword" to the computer cookies. After clicking button, script should be check my login and my password and save "mypassword" to the session cookies (to receive on page "admin.aspx") and redirect to "admin.aspx". If "password" was already written in computer cookies or in server cookies (when I have already sign in this session). But I receive the error: ‘An unhandled exception occurred during the execution of the current web request. System.NullReferenceException: Object reference not set to an instance of an object’.

    Here whole code of the script:

    <%@ Page Language="c#"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><script type="text/c#" runat="server" language="c#">

     // Private variables:

        private string clogin;

        private string cpassword;

    //When page load:

        protected void Page_Load(object sender, EventArgs e)

        {

    // Content of private variables, correct login and password:

            clogin = "szmitek";

     

            cpassword = "mypasswod";

    // If ‘mypassword’ (content of variable cpassword) was already written in computer or session cookies (it cause error; line 13):

            if (Request.Cookies["login"]["password"] == cpassword || Session["password"] == cpassword)

            {

    // Automated redirecting:

                Response.Redirect("admin.aspx");

            }

     

        }

    // After clicking button:

        protected void SubmitBtn_Click(object sender, EventArgs e)

        {

    // If text from textboxes login and password was correct:

            if (login.Text == clogin && password.Text == cpassword)

            {

    // Adding ‘mypassword’ to session cookies:

                Session["password"] = cpassword;

    // Adding ‘mypassword’ to computer cookies when checkbox is checked:

                if (CheckBox.Checked == true)

                {

                  Response.Cookies["login"].Domain = "szmitek.winweb.pl";

                  Response.Cookies["login"]["password"] = cpassword;

                }

    // Redirecting:

                Response.Redirect("admin.aspx");

            }

            else

            {

    // Redirecting when text from texboxes is not correct:

                Response.Redirect("fail.aspx");

            }

           

        } </script>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Administracja - logowanie</title><link rel="stylesheet" type="text/css" href="../format.css" /></head><body><h1>Zaloguj si&#281;</h1><div class="center">

    // Form:

    <form runat="server" method="post" action="index.aspx">Login:

    <asp:TextBox ID="login" runat="server"></asp:TextBox><br />Has&#322;o:

    <asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox><br />

    //  Checkbox. If it is checked, ‘mypassword’  will be save in computer cookies:

    <asp:CheckBox ID="CheckBox" Text="Loguj automatycznie w tym systemie" runat="server" /><br /><asp:Button id="Button1" runat="server" text="Zaloguj si&#281;" onclick="SubmitBtn_Click" /></form></div></body></html>

    How to resolve this problem? Am I receiving session cookies in correct way (Session[“passwod”]).

    And this is fragment of error message:

    Object reference not set to an instance of an object.

    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.

    Source Error:

     

    Line 11:         cpassword = "bcvnma";

    Line 12:                                                                                              

    Line 13:         if (Request.Cookies["login"]["password"] == cpassword || Session["password"] == cpassword)

    Line 14:         {

    Line 15:             Response.Redirect("admin.aspx");




    Stack Trace:

     

    [NullReferenceException: Object reference not set to an instance of an object.]

       ASP.admin_index_aspx.Page_Load(Object sender, EventArgs e) in i:\Users\Administrator\Documents\My Web Sites\Kamil Szmit (szmitek)\admin\index.aspx:13

       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15

       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34

       System.Web.UI.Control.OnLoad(EventArgs e) +99

       System.Web.UI.Control.LoadRecursive() +47

       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

    What cause the problem? How to receive session cookie (added by ‘Session[“password”] = cpassword’) in good, correct way?

  • Re: [c#, session cookies]Object reference not set to an instance of an object. How to receive session cookie (added by ‘Session[“password”] = cpassword’) in good, correct way?

    05-13-2007, 3:21 PM
    Answer
    • Loading...
    • vivek_iit
    • Joined on 06-18-2006, 2:13 PM
    • New Delhi
    • Posts 3,072
    • TrustedFriends-MVPs

    Hi,

    You need to check for nulls first before comparison, like:

    if (Request["x"] != null && Session["x"] != null)

    //rest of the code

    Hope this helps,

    Vivek
     

    MVP, ASP.NET || My Website || Blog || Articles

    Please mark the most helpful reply/replies as "Answer".
  • Sad [:(] Re: [c#, session cookies]Object reference not set to an instance of an object. How to receive session cookie (added by ‘Session[“x”] = y’) in correct way?

    05-19-2007, 3:37 PM
    • Loading...
    • szmitek
    • Joined on 03-04-2007, 12:22 AM
    • Chotomów, Poland
    • Posts 59

    I have added this to index.aspx:

    protected void Page_Load(object sender, EventArgs e)

    {

     

    cpassword = "my_password";

    if (Request.Cookies["login"]["password"] != null || Session["password"] != null)

    {

    if (Request.Cookies["login"]["password"] == cpassword || Session["password"] == cpassword)

    {

    Response.Redirect("admin.aspx");

    }

    }

    clogin = "szmitek";

    }

    And this to admin.aspx:

    <script runat="server" type="text/c#">

    void Page_Load(object src, EventArgs e)

    {

    if (Request.Cookies["login"]["password"] != null || Session["password"] != null)

    {

    if (Request.Cookies["login"]["password"] != "my_password" || Session["password"] != "my_passwod")

    {

    Response.Redirect(
    "index.aspx");

    }

    }

    else

    {

    Response.Redirect(
    "index.aspx");

    }

    }

    </script>

    This cause to 2 errors:

    1. When Session["password"] is not null, the same error occures as before ("Object reference not set to an instance of an object");
    2. Content of Session["password"] is not being deleted after closing browser window.

    How to resolve this problem? How to respond and request information on varied pages which is deleted after closing browser window (during one session) in another way?

  • Re: [c#, session cookies]Object reference not set to an instance of an object. How to receive session cookie (added by ‘Session[“x”] = y’) in correct way?

    05-21-2007, 1:57 AM
    • Loading...
    • vivek_iit
    • Joined on 06-18-2006, 2:13 PM
    • New Delhi
    • Posts 3,072
    • TrustedFriends-MVPs

    The error #1 might be occuring as Request object might be null. You are using an OR (||) condition so if any one object out of Session and Response is not null the code falls into the loop. So you should use the AND condition instead while checking for the nulls (like if(Request[x] != null && Session[x]!=null))

    Also note that Session objects are not deleted automatically as you close the window. They will expire when you call Session.Abandon() or when session timeout occurs (specified the the config file).

    Hope this helps,

    Vivek

    MVP, ASP.NET || My Website || Blog || Articles

    Please mark the most helpful reply/replies as "Answer".
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter