473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.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: }


Jun 6 '06 #1
4 1273
PageLoad event fires before OnClick one. That's why Session["UserName"] is
null in PageLoad.

Eliyahu

"Gunawan" <jg**********@gmail.com> wrote in message
news:eQ**************@TK2MSFTNGP05.phx.gbl...
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: }


Jun 6 '06 #2
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
Jun 6 '06 #3
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" <re*************@monarchmed.com> wrote in message
news:e2**************@TK2MSFTNGP03.phx.gbl...
PageLoad event fires before OnClick one. That's why Session["UserName"] is
null in PageLoad.

Eliyahu

"Gunawan" <jg**********@gmail.com> wrote in message
news:eQ**************@TK2MSFTNGP05.phx.gbl...
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: }



Jun 6 '06 #4
Can you put the redirecting statement in the button1 onclick?

Eliyahu

"Gunawan" <jg**********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:e2**************@TK2MSFTNGP03.phx.gbl...
PageLoad event fires before OnClick one. That's why Session["UserName"]
is null in PageLoad.

Eliyahu

"Gunawan" <jg**********@gmail.com> wrote in message
news:eQ**************@TK2MSFTNGP05.phx.gbl...
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: }




Jun 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
7
by: Ottar | last post by:
I've made a program sorting incomming mail in public folder. The function runs every minute by using the form.timer event. In Access XP it runs for weeks, no problem. Access 2003 runs the same...
2
by: James Wallace | last post by:
I hope that someone can help me out there with this problem. I get an itermittant problem with our web page that occurs about once every 10 to 15 days where the only way to fix the problem is to...
2
by: Ricardo Magalhães | last post by:
Hi, I using this code, to connect with a paradox file with ASP.NET. The first time its connect ok, but others times occurs the error: "ERROR - no error information available" I close all...
11
by: Thomas Zangl | last post by:
Hi! I have a class hierachie like this: // interface, abstract only class ISession { } // implements common parts of ISession
11
by: Jerry | last post by:
I'm using this code: Dim strName For Each strName in Session.Contents Response.Write strName & " - " & Session.Contents(strName) & "<BR>" Next If I only do a response.write strName, it shows...
2
by: arun1985 | last post by:
In the project i am using i am having the following code and when i upload it to the server.Its givig me the following error in the global.cs file. Server Error in '/' Application. ...
3
by: sjohnson1984 | last post by:
Hello all, thanks for taking the time out to read this. I have a javascript function, the purpose of which is to compare 2 form values and redirect the user if two values are met - however,...
4
by: Chronictank | last post by:
Hi, as a bit of background (and seeing as it is my first post :)) i am a complete newbie at perl. I literally picked it up a week ago to do this project as it seemed like the best choice for a...
2
by: Thefire | last post by:
Hello All i am using Bisness object Crystal Report XI version and installed it on my server. I have checked to installed as server. I am using the following code to connect and display the Report...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.