sansactions
sansactions

Reputation: 243

Problems with Session losing its data in .Net

I have allready asked this question before, but now I have found more details and some more weird stuf.

I have a web project that has 2 .aspx files that connect data with sessions. Without any extra code this works, when i add my extra code the session no longer works. Does anybody have an idea why?

Code where sessions work:

Form1:

protected void Page_Load(object sender, EventArgs e)
{
    Session["data"] = "5";
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
    string s = (string)(Session["data"]); // 5 when i look while debugging
    Response.Redirect("~/Form2.aspx", false);
}

Form 2:

protected void Page_Load(object sender, EventArgs e)
{
    string s = (string)(Session["data"]); // s = 5 when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
    string s = (string)(Session["data"]); // s = 5 when i look while debugging
}

Code where sessions don't work: (i use 2 classlibraries (logic and dataAccess where I get data from json webservice and parse it to my forms). Form1:

protected void Page_Load(object sender, EventArgs e)
{
    Logic logic = new Logic();
    logic.login(credentials);
    List<AppointmentExtParticipant> opleidingVolgers = logic.getOpleidingVolgers();
    foreach (AppointmentExtParticipant app in opleidingVolgers)
    {
        if (app.contact != null)
        {
            Relation rel = logic.getRelationData(app.contact.FK_RELATION);
            DropDownListUsers.Items.Add(app.ToString() + " " + rel.ToString());
        }
    }
    Session["data"] = "5";
}

protected void ButtonOk_Click(object sender, EventArgs e)
{
    string s = (string)(Session["opleidingvolger"]); // s = 5 when i look while debugging
    Response.Redirect("~/Form2.aspx", false);
}

Form 2:

protected void Page_Load(object sender, EventArgs e)
{
    string s = (string)(Session["data"]); // s = null when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
    string s = (string)(Session["data"]); // s = null when i look while debugging
}    

Ofcourse I simplified the names here a bit so people could understand, thnx!

edit:

logic login: enter image description here dataaccess login: enter image description here

Here i get data from my webservice hosted on another url.

Upvotes: 0

Views: 48

Answers (1)

Usman
Usman

Reputation: 4693

Sessions generally work by using cookies.You are using HttpWebRequests which creates new sessionSo you need to preserve session cookie between requests. Thats why we use CookieContainer so add

 CookieContainer container = new CookieContainer();
HttpWebRequest req = WebRequest.Create(
             "")  as HttpWebRequest;
 req.CookieContainer = container;

Upvotes: 1

Related Questions