session variables

P

puja

hi all,

I have a problem. I have a class file name Agent which has Name and
SalesPerson as two strings. On below page, am reading session object called
agentdetail of class Agent and filling out agency name and salesperson name
on page load event

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

{

Agent agentdetail = new Agent();

protected void Page_Load(object sender, EventArgs e)

{ if (Session["agentdetail"] != null)

{ agentdetail = (Agent)Session["agentdetail"];

agencyname.Text = agentdetail.Name;

asalesperson.Text = agentdetail.SalesPerson;

}}

}

Now on button called proceed, am saving agency name and sales person name
again in agentdetail object and adding it into session variable called
"agentdetail". Now if I come back to this page and change agencyname or
sales person name then its not saving changes to the new object in the
session. Eg - if I have "abc" as agency name and "myagent" as agent name
which on proceed button, I save it in session varialbe called agentdetail.
Now when I come back to page, it shows correct details and if I change sales
person name to "youragent" and them comee back to this page, it still shows
"myagent" .....

protected void proceed_Click(object sender, EventArgs e)

{ agentdetail.Name = agencyname.Text;

agentdetail.SalesPerson = asalesperson.Text;

Session.Add("agentdetail", agentdetail); // in short here it is
not replacing the agentdetail variable which is alredy stored in session.

}

I don't know where am going wrong. Pls help...........(am kinda new to c#)

here is the structure of Address class file

public class Agent

{

private string name;

private string salesPerson;

public string Name

{

get { return this.name; }

set { this.name = value; }

}


public string SalesPerson

{

get { return this.salesPerson; }

set { this.salesPerson = value; }

}



}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

The problem is not with storing the value in the session variable. That
part of the code is working fine.

The problem is that the Page_Load method is called before the
proceed_Click method, so you will be getting the old values from the
session variable and putting them in the text boxes. the procees_Click
method then takes those values and successfully put them back in the
session variable.

Make the code in the Page_Load method check the value of Page.IsPostback
to determine if the text boxes should be populated or not.
 

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

Top