I want to store a sequence input of number separate by comma (1,2,3,4…)
into a session object
And my code is:
string items = string.Empty;
protected void Button1_Click(object sender, EventArgs e)
{
fillitem(TextBox1.Text);
showsession();
}
void fillitem(string par1)
{
if (Session["itemvar"] != null)
{
items = Session["itemvar"].ToString();
Session["itemvar"] = null;
}
items += par1 + ",";
Session["itemvar"] = items;
}
void showsession()
{
string itempp = string.Empty;
if (Session["itemvar"] != null)
{
itempp = Session["itemvar"].ToString();
int indexOfkoma = itempp.LastIndexOf(",", StringComparison.OrdinalIgnoreCase);
if (indexOfkoma != -1)
{
itempp = itempp.Substring(0, indexOfkoma);
}
}
Label1.Text = itempp;
}
The code is running wel on my local. But when I publish on the server, the session object returns strange results like below transaction:
Button1_Click Event 1 Result :
Session[“itemvar”] Return : 1
Button1_Click Event 2 Result :
Session[“itemvar”] Return : 2 —-> The correct result should return: 1,2
Button1_Click Event 3 Result :
Session[“itemvar”] Return : 1,3 —-> The correct result should return: 1,2,3
Button1_Click Event 4 Result :
Session[“itemvar”] Return : 2,4 —-> The correct result should return: 1,2,3,4
Any idea, what is the cause of these strange behavior?
The behavior you are seeing could be caused by using In-Process Session State (default on IIS) in a web farm configuration.
Your requests in such a case would actually be serviced by different application servers(each with its own session).
Configure Out-of-Process Session State to solve this problem in a web farm scenario.