Error in Session

G

Gunawan

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.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class one : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

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

{

Session["FirstLoadTime"] = DateTime.Now.ToString("T");

}

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



if (IsPostBack)

{

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

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

}

}

protected void Button1_Click(object 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.NullReferenceException: 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: }
 
G

Gunawan

If I Using this code
using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class one : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string sUserName;

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

{

Session["FirstLoadTime"] = DateTime.Now.ToString("T");

}

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



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

{

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

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

+ sUserName + "!"

+"<br> IsPostBack: " + IsPostBack.ToString();

}

else

{

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

+"<br> IsPostBack: " + IsPostBack.ToString();

}

}

protected void Button1_Click(object sender, EventArgs e)

{

Session["UserName"] = TextBox1.Text;



}

}



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

Why? any idea?



TIA

Gun
 
G

Gunawan

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 said:
PageLoad event fires before OnClick one. That's why Session["UserName"] is
null in PageLoad.

Eliyahu

Gunawan said:
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.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class one : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

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

{

Session["FirstLoadTime"] = DateTime.Now.ToString("T");

}

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



if (IsPostBack)

{

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

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

}

}

protected void Button1_Click(object 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.NullReferenceException: 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: }
 
E

Eliyahu Goldin

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

Eliyahu
 
E

Eliyahu Goldin

Can you put the redirecting statement in the button1 onclick?

Eliyahu

Gunawan said:
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 said:
PageLoad event fires before OnClick one. That's why Session["UserName"]
is null in PageLoad.

Eliyahu

Gunawan said:
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.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;



public partial class one : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

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

{

Session["FirstLoadTime"] = DateTime.Now.ToString("T");

}

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



if (IsPostBack)

{

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

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

}

}

protected void Button1_Click(object 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.NullReferenceException: 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: }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top