Session State in C#

G

grecci

I'm fairly new to the c# language.... I want to be able to retrieve info
from page 1 and carry it over page 3,4,5... then finally put into a
database... tell me what's wrong with this simple codes...

source.aspx
protected void Subitrequest_Click(object sender, EventArgs e)
{
Session["name"] = DropDownList8.Text;

}

----------------------------------------------------------
Target.aspx
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"];

Response.Write(Label1.Text);
}

What I want to do is extremely simple, retrieve all sort of info from
textboxes and dropdownlists from source.aspx and carry them over to
other pages ....Again I'm using asp.net with C# ....

Thanks G.
 
L

Laurent Bugnion

Hi,
I'm fairly new to the c# language.... I want to be able to retrieve info
from page 1 and carry it over page 3,4,5... then finally put into a
database... tell me what's wrong with this simple codes...

source.aspx
protected void Subitrequest_Click(object sender, EventArgs e)
{
Session["name"] = DropDownList8.Text;

}

----------------------------------------------------------
Target.aspx
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"];

Response.Write(Label1.Text);
}

What I want to do is extremely simple, retrieve all sort of info from
textboxes and dropdownlists from source.aspx and carry them over to
other pages ....Again I'm using asp.net with C# ....

Thanks G.

There is nothing wrong with your code, but you must check if the Session
variable exists, or else you risk that it is null and throws an
exception when used.

Additionally, using IDs hardcoded is bad practice. Save the IDs (like
"name") as constants in one of the pages, and you can access them from
the other pages.

HTH,
Laurent
 
M

Masudur

Hi,

Laurent is right.
Try

if (Session["name"]==null)
Session.Add("name",yourValue);
else
Session["name"]=yourValue;

.....
And its better to use constent string or enum.... as the "key" of the
session state


Masudur
Kaz Software Ltd.
www.kaz.com.bd
 
L

Laurent Bugnion

Hi,
Hi,

Laurent is right.
Try

if (Session["name"]==null)
Session.Add("name",yourValue);
else
Session["name"]=yourValue;

Unnecessary.

Session[ "name" ] = value;
and
Session.Add( "name", value );

are equivalent.
....
And its better to use constent string or enum.... as the "key" of the
session state

No, the key for the Session object must be a string.

HTH,
Laurent
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top