SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2005
    Location
    N-472'26" W-12249'34"
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session variable from .aspx page to .cs class?

    I've worked with classic ASP and know C by way of Java and a few simple web services, at my new just they have a .net 2003 web app and I've been able to help fix up a few things as I'm getting up to speed, but I have one place where I need to get a value from the .aspx page into the validation code in a C# class.

    I don't want to include the value in the method call, because I would then have to include the data in every subsequent method call (a couple dozen, maybe more) for each validation until I hit the save at the bottom.

    I set a session variable in the .aspx page and watched it get set in the debugger, and know it's there for the other code, but when I try to reference it in the C# method, I get the build error:
    "The name 'Session' does not exist in the class or namespace ..."

    I can't find any code examples to figure this out, can anybody help me with this?

    Thanks in advance,
    Josh

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HttpContext.Current.Session

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Though you really should create an object that wraps these variables and place that in the session as well as ship that around to your various methods.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2005
    Location
    N-472'26" W-12249'34"
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, by the way, HoneyMonster. This led me to what I needed.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Nigeria
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which was what?

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2005
    Location
    N-472'26" W-12249'34"
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The actual code I used was:
    Code:
    intCompanyID = Int32.Parse(System.Web.HttpContext.Current.Session["session_intCompanyID"].ToString());
    and it works like a charm.

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    But what happens when System.Web.HttpContext.Current is null?

    You are better off passing parameters into a method and not having this dependence upon a singleton floating around. Yes, I learned that the hard way.

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^^^ listen to what W says (our own resident W; who is a lot more informed and speaks from experience; not from belief).

  9. #9
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Who says I'm not divinely inspired?

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2005
    Location
    N-472'26" W-12249'34"
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes yes, this I know, but the way this app works (I didn't write it, I just got hired to finish it) is to call the code behind with a validate method to validate a data element against their (often conflicting, I'm finding) business rules. On success, the parameters are then passed to a second method for another element or rule, and so on until the whole dataset is verified. There's a couple dozen methods that pass these parameters on and I'd have to add this data element to each, they're not always called in the same order, depending on what's updated, and one misstep could cause them all to fail. A single floater (which is only required because now they want to ignore one of the business rules completely) seems like the lesser evil. (and definitely less of a maintenance headache)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •