Session State in C#

  • Thread starter Thread starter grecci
  • Start date Start date
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.
 
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
 
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
 
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
 
Back
Top