Error in Session

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gunawan

    #1

    Error in Session

    Hi All,
    I am using VS 2005 to build this page.
    One the code file I use this code
    using System;

    using System.Data;

    using System.Configur ation;

    using System.Collecti ons;

    using System.Web;

    using System.Web.Secu rity;

    using System.Web.UI;

    using System.Web.UI.W ebControls;

    using System.Web.UI.W ebControls.WebP arts;

    using System.Web.UI.H tmlControls;



    public partial class one : System.Web.UI.P age

    {

    protected void Page_Load(objec t sender, EventArgs e)

    {

    if (Session["FirstLoadT ime"] == null)

    {

    Session["FirstLoadT ime"] = DateTime.Now.To String("T");

    }

    Label1.Text = "Page first loaded at " +
    Session["FirstLoadT ime"].ToString();



    if (IsPostBack)

    {

    Label1.Text += "<br> Welcome back, "

    + Session["UserName"].ToString();

    }

    }

    protected void Button1_Click(o bject sender, EventArgs e)

    {

    if (Session["UserName"] == null)

    {

    Session["UserName"] = TextBox1.Text;

    }



    }

    }



    But why I always get this error though I have type something in the textbox1





    Server Error in '/WebData' Application.
    --------------------------------------------------------------------------------

    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.NullRefe renceException: Object reference not set
    to an instance of an object.

    Source Error:

    Line 22: if (IsPostBack)
    Line 23: {
    Line 24: Label1.Text += "<br> Welcome back, "
    Line 25: + Session["UserName"].ToString();
    Line 26: }






  • Eliyahu Goldin

    #2
    Re: Error in Session

    PageLoad event fires before OnClick one. That's why Session["UserName"] is
    null in PageLoad.

    Eliyahu

    "Gunawan" <jgun98.milis@g mail.com> wrote in message
    news:eQa$MTTiGH A.4572@TK2MSFTN GP05.phx.gbl...[color=blue]
    > Hi All,
    > I am using VS 2005 to build this page.
    > One the code file I use this code
    > using System;
    >
    > using System.Data;
    >
    > using System.Configur ation;
    >
    > using System.Collecti ons;
    >
    > using System.Web;
    >
    > using System.Web.Secu rity;
    >
    > using System.Web.UI;
    >
    > using System.Web.UI.W ebControls;
    >
    > using System.Web.UI.W ebControls.WebP arts;
    >
    > using System.Web.UI.H tmlControls;
    >
    >
    >
    > public partial class one : System.Web.UI.P age
    >
    > {
    >
    > protected void Page_Load(objec t sender, EventArgs e)
    >
    > {
    >
    > if (Session["FirstLoadT ime"] == null)
    >
    > {
    >
    > Session["FirstLoadT ime"] = DateTime.Now.To String("T");
    >
    > }
    >
    > Label1.Text = "Page first loaded at " +
    > Session["FirstLoadT ime"].ToString();
    >
    >
    >
    > if (IsPostBack)
    >
    > {
    >
    > Label1.Text += "<br> Welcome back, "
    >
    > + Session["UserName"].ToString();
    >
    > }
    >
    > }
    >
    > protected void Button1_Click(o bject sender, EventArgs e)
    >
    > {
    >
    > if (Session["UserName"] == null)
    >
    > {
    >
    > Session["UserName"] = TextBox1.Text;
    >
    > }
    >
    >
    >
    > }
    >
    > }
    >
    >
    >
    > But why I always get this error though I have type something in the
    > textbox1
    >
    >
    >
    >
    >
    > Server Error in '/WebData' Application.
    > --------------------------------------------------------------------------------
    >
    > 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.NullRefe renceException: Object reference not set
    > to an instance of an object.
    >
    > Source Error:
    >
    > Line 22: if (IsPostBack)
    > Line 23: {
    > Line 24: Label1.Text += "<br> Welcome back, "
    > Line 25: + Session["UserName"].ToString();
    > Line 26: }
    >
    >
    >
    >
    >
    >[/color]


    Comment

    • Gunawan

      #3
      Re: Error in Session

      If I Using this code
      using System;

      using System.Data;

      using System.Configur ation;

      using System.Collecti ons;

      using System.Web;

      using System.Web.Secu rity;

      using System.Web.UI;

      using System.Web.UI.W ebControls;

      using System.Web.UI.W ebControls.WebP arts;

      using System.Web.UI.H tmlControls;



      public partial class one : System.Web.UI.P age

      {

      protected void Page_Load(objec t sender, EventArgs e)

      {

      string sUserName;

      if (Session["FirstLoadT ime"] == null)

      {

      Session["FirstLoadT ime"] = DateTime.Now.To String("T");

      }

      Label1.Text = "Page first loaded at " +
      Session["FirstLoadT ime"].ToString();



      if (IsPostBack && Session["UserName"] != null)

      {

      sUserName = (string)Session["UserName"];

      Label1.Text += "<br> Welcome back, "

      + sUserName + "!"

      +"<br> IsPostBack: " + IsPostBack.ToSt ring();

      }

      else

      {

      Label1.Text += "<br> Welcome back, unknown user."

      +"<br> IsPostBack: " + IsPostBack.ToSt ring();

      }

      }

      protected void Button1_Click(o bject sender, EventArgs e)

      {

      Session["UserName"] = TextBox1.Text;



      }

      }



      I need click button1 twice to make username appear on label1.

      Why? any idea?



      TIA

      Gun


      Comment

      • Gunawan

        #4
        Re: Error in Session

        I see.... Now I can figure out what happened.

        I would to know what is the best practice if I want to do something like
        this

        If user click button1 on one.aspx I would like to perform some action before
        directing user to another page (two.aspx).
        Action that I want to perform effecting Session["DataMS"]
        Session["DataMS"] use in page two.aspx.

        does button1 onclick perform before pageload on two.aspx?

        TIA
        Gun

        "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
        news:e2fqoeTiGH A.1276@TK2MSFTN GP03.phx.gbl...[color=blue]
        > PageLoad event fires before OnClick one. That's why Session["UserName"] is
        > null in PageLoad.
        >
        > Eliyahu
        >
        > "Gunawan" <jgun98.milis@g mail.com> wrote in message
        > news:eQa$MTTiGH A.4572@TK2MSFTN GP05.phx.gbl...[color=green]
        >> Hi All,
        >> I am using VS 2005 to build this page.
        >> One the code file I use this code
        >> using System;
        >>
        >> using System.Data;
        >>
        >> using System.Configur ation;
        >>
        >> using System.Collecti ons;
        >>
        >> using System.Web;
        >>
        >> using System.Web.Secu rity;
        >>
        >> using System.Web.UI;
        >>
        >> using System.Web.UI.W ebControls;
        >>
        >> using System.Web.UI.W ebControls.WebP arts;
        >>
        >> using System.Web.UI.H tmlControls;
        >>
        >>
        >>
        >> public partial class one : System.Web.UI.P age
        >>
        >> {
        >>
        >> protected void Page_Load(objec t sender, EventArgs e)
        >>
        >> {
        >>
        >> if (Session["FirstLoadT ime"] == null)
        >>
        >> {
        >>
        >> Session["FirstLoadT ime"] = DateTime.Now.To String("T");
        >>
        >> }
        >>
        >> Label1.Text = "Page first loaded at " +
        >> Session["FirstLoadT ime"].ToString();
        >>
        >>
        >>
        >> if (IsPostBack)
        >>
        >> {
        >>
        >> Label1.Text += "<br> Welcome back, "
        >>
        >> + Session["UserName"].ToString();
        >>
        >> }
        >>
        >> }
        >>
        >> protected void Button1_Click(o bject sender, EventArgs e)
        >>
        >> {
        >>
        >> if (Session["UserName"] == null)
        >>
        >> {
        >>
        >> Session["UserName"] = TextBox1.Text;
        >>
        >> }
        >>
        >>
        >>
        >> }
        >>
        >> }
        >>
        >>
        >>
        >> But why I always get this error though I have type something in the
        >> textbox1
        >>
        >>
        >>
        >>
        >>
        >> Server Error in '/WebData' Application.
        >> --------------------------------------------------------------------------------
        >>
        >> 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.NullRefe renceException: Object reference not
        >> set to an instance of an object.
        >>
        >> Source Error:
        >>
        >> Line 22: if (IsPostBack)
        >> Line 23: {
        >> Line 24: Label1.Text += "<br> Welcome back, "
        >> Line 25: + Session["UserName"].ToString();
        >> Line 26: }
        >>
        >>
        >>
        >>
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Eliyahu Goldin

          #5
          Re: Error in Session

          Can you put the redirecting statement in the button1 onclick?

          Eliyahu

          "Gunawan" <jgun98.milis@g mail.com> wrote in message
          news:%23hlJ35Ti GHA.4716@TK2MSF TNGP03.phx.gbl. ..[color=blue]
          >I see.... Now I can figure out what happened.
          >
          > I would to know what is the best practice if I want to do something like
          > this
          >
          > If user click button1 on one.aspx I would like to perform some action
          > before directing user to another page (two.aspx).
          > Action that I want to perform effecting Session["DataMS"]
          > Session["DataMS"] use in page two.aspx.
          >
          > does button1 onclick perform before pageload on two.aspx?
          >
          > TIA
          > Gun
          >
          > "Eliyahu Goldin" <removemeegoldi n@monarchmed.co m> wrote in message
          > news:e2fqoeTiGH A.1276@TK2MSFTN GP03.phx.gbl...[color=green]
          >> PageLoad event fires before OnClick one. That's why Session["UserName"]
          >> is null in PageLoad.
          >>
          >> Eliyahu
          >>
          >> "Gunawan" <jgun98.milis@g mail.com> wrote in message
          >> news:eQa$MTTiGH A.4572@TK2MSFTN GP05.phx.gbl...[color=darkred]
          >>> Hi All,
          >>> I am using VS 2005 to build this page.
          >>> One the code file I use this code
          >>> using System;
          >>>
          >>> using System.Data;
          >>>
          >>> using System.Configur ation;
          >>>
          >>> using System.Collecti ons;
          >>>
          >>> using System.Web;
          >>>
          >>> using System.Web.Secu rity;
          >>>
          >>> using System.Web.UI;
          >>>
          >>> using System.Web.UI.W ebControls;
          >>>
          >>> using System.Web.UI.W ebControls.WebP arts;
          >>>
          >>> using System.Web.UI.H tmlControls;
          >>>
          >>>
          >>>
          >>> public partial class one : System.Web.UI.P age
          >>>
          >>> {
          >>>
          >>> protected void Page_Load(objec t sender, EventArgs e)
          >>>
          >>> {
          >>>
          >>> if (Session["FirstLoadT ime"] == null)
          >>>
          >>> {
          >>>
          >>> Session["FirstLoadT ime"] = DateTime.Now.To String("T");
          >>>
          >>> }
          >>>
          >>> Label1.Text = "Page first loaded at " +
          >>> Session["FirstLoadT ime"].ToString();
          >>>
          >>>
          >>>
          >>> if (IsPostBack)
          >>>
          >>> {
          >>>
          >>> Label1.Text += "<br> Welcome back, "
          >>>
          >>> + Session["UserName"].ToString();
          >>>
          >>> }
          >>>
          >>> }
          >>>
          >>> protected void Button1_Click(o bject sender, EventArgs e)
          >>>
          >>> {
          >>>
          >>> if (Session["UserName"] == null)
          >>>
          >>> {
          >>>
          >>> Session["UserName"] = TextBox1.Text;
          >>>
          >>> }
          >>>
          >>>
          >>>
          >>> }
          >>>
          >>> }
          >>>
          >>>
          >>>
          >>> But why I always get this error though I have type something in the
          >>> textbox1
          >>>
          >>>
          >>>
          >>>
          >>>
          >>> Server Error in '/WebData' Application.
          >>> --------------------------------------------------------------------------------
          >>>
          >>> 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.NullRefe renceException: Object reference not
          >>> set to an instance of an object.
          >>>
          >>> Source Error:
          >>>
          >>> Line 22: if (IsPostBack)
          >>> Line 23: {
          >>> Line 24: Label1.Text += "<br> Welcome back, "
          >>> Line 25: + Session["UserName"].ToString();
          >>> Line 26: }
          >>>
          >>>
          >>>
          >>>
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          Working...