Session state from one page

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2750 Views - Last Post: 09 October 2012 - 02:19 PM Rate Topic: -----

#16 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Session state from one page

Posted 09 October 2012 - 08:13 AM

I have tried doing that, plus the "+ name" method and both wouldn't show the value in the Session. I tried debugging via Add watch and it still says it's empty or null. Could it be the page order in the solution explorer?

I also did a check to see if the Seesion was null and if it was null a message would appear. Which means that the Session doesn't have anything stored in it even though I have it coded in from the original page:

string name = txtName.Text;

Session["NAMES"] = name;




That occurs on the button event. But when I debugged the Session["NAMES" = name; line it said that it was empty.

This post has been edited by jaArch: 09 October 2012 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#17 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,460
  • Joined: 07-April 08

Re: Session state from one page

Posted 09 October 2012 - 08:47 AM

It said what was empty? name or Session["Names"]? Session["Names"] will show null until AFTER the line where you set it's value. The order in which the pages show up in the solution explorer doesn't matter at all. Session is a global thing so it will persist across all your pages until it expires. You said that you setup something that checks Session["Names"] for being null. Where did you put that, and is it being fired?
Was This Post Helpful? 0
  • +
  • -

#18 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Session state from one page

Posted 09 October 2012 - 08:58 AM

It said that Session["Names"]; was empty here:

string name = txtName.Text;
Session["Names"] = name;



I put that on the page where after you click the button it takes you to the next page.

string name = (string)Session["Names"];

if (Session["Names"] == null){
lblError.Text = "null"; 
}


It displays "null" in the label after I test it.

Shouldn't it store the value entered from the textbox into the session variable? Cause it doesn't seem to be doing that.

This post has been edited by jaArch: 09 October 2012 - 09:00 AM

Was This Post Helpful? 0
  • +
  • -

#19 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,460
  • Joined: 07-April 08

Re: Session state from one page

Posted 09 October 2012 - 10:21 AM

I think i am starting to understand the problem. Post the entire click event. Posting little snippets doesn't give us the full picture. Once I see the entire click event I will be able to form a better understanding.
Was This Post Helpful? 0
  • +
  • -

#20 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Session state from one page

Posted 09 October 2012 - 10:44 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        compAge.ValueToCompare = DateTime.Now.ToString("d");

    }
    protected void btnPlay_Click(object sender, EventArgs e)
    {
        DateTime birthDate;

        if (DateTime.TryParse(txtBirth.Text, out birthDate))
        {
            int year = DateTime.Today.Year - Convert.ToDateTime(txtBirth.Text).Year;

            Session["YEAR"] = year;
        }
        Session["NAME"] = txtName.Text;
    }
}


Was This Post Helpful? 0
  • +
  • -

#21 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,460
  • Joined: 07-April 08

Re: Session state from one page

Posted 09 October 2012 - 10:51 AM

that can't be the entire thing, where do you redirect to the next page?

I want to see the entire event where this: string name = txtName.Text;
Session["Names"] = name;
is at.
Was This Post Helpful? 0
  • +
  • -

#22 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Session state from one page

Posted 09 October 2012 - 01:11 PM

Oh, sorry about that.

protected void btnPlay_Click(object sender, EventArgs e)
    {
        string name = txtName.Text;
        Session["name"] = name;
    }


I redirect to the next page in the button property area.
Was This Post Helpful? 0
  • +
  • -

#23 jaArch  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 217
  • Joined: 23-March 10

Re: Session state from one page

Posted 09 October 2012 - 01:34 PM

Ok, wow. Now I see what was wrong...I was supposed to use Respons.Redirect...not postbackurl. Tanks for the help!
Was This Post Helpful? 0
  • +
  • -

#24 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: Session state from one page

Posted 09 October 2012 - 02:19 PM

Are both of those btnPlay_Click event handlers on the same page?

This post has been edited by h4nnib4l: 09 October 2012 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2