Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Is there a way to check that a Session variable exists without using try/catch?

Is there a way to check that a Session variable exists without using try/catch?

Scheduled Pinned Locked Moved ASP.NET
question
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Red_Wizard_Shot_The_Food
    wrote on last edited by
    #1

    Is there a nice easy way to check Session["x"] exists without trying and catching it? Thanks.

    _ 1 Reply Last reply
    0
    • R Red_Wizard_Shot_The_Food

      Is there a nice easy way to check Session["x"] exists without trying and catching it? Thanks.

      _ Offline
      _ Offline
      _AK_
      wrote on last edited by
      #2

      no need to use try catch for that they are there for different purpose. You can check the session like this: if(Session["x"]!=null) { //do your work. }

      Best Regards, Apurva Kaushal

      R 1 Reply Last reply
      0
      • _ _AK_

        no need to use try catch for that they are there for different purpose. You can check the session like this: if(Session["x"]!=null) { //do your work. }

        Best Regards, Apurva Kaushal

        R Offline
        R Offline
        Red_Wizard_Shot_The_Food
        wrote on last edited by
        #3

        Oh my I feel like an idoit! I could have sword i tried that and it gave me an error for referencing a somthing that didn't exist but that error was caused by another bug :doh: Thanks for pointing it out for me :)

        _ 1 Reply Last reply
        0
        • R Red_Wizard_Shot_The_Food

          Oh my I feel like an idoit! I could have sword i tried that and it gave me an error for referencing a somthing that didn't exist but that error was caused by another bug :doh: Thanks for pointing it out for me :)

          _ Offline
          _ Offline
          _AK_
          wrote on last edited by
          #4

          Always welcome.. :)

          Best Regards, Apurva Kaushal

          R 1 Reply Last reply
          0
          • _ _AK_

            Always welcome.. :)

            Best Regards, Apurva Kaushal

            R Offline
            R Offline
            Ramasubramaniam
            wrote on last edited by
            #5

            Imagine the key has not been added on the session at all, that time do we need to user only try...catch block or is there any other way will work? Ram

            _ 1 Reply Last reply
            0
            • R Ramasubramaniam

              Imagine the key has not been added on the session at all, that time do we need to user only try...catch block or is there any other way will work? Ram

              _ Offline
              _ Offline
              _AK_
              wrote on last edited by
              #6

              No in that case also you can check the session like that(what I mention). :)

              Best Regards, Apurva Kaushal

              R 1 Reply Last reply
              0
              • _ _AK_

                No in that case also you can check the session like that(what I mention). :)

                Best Regards, Apurva Kaushal

                R Offline
                R Offline
                Ramasubramaniam
                wrote on last edited by
                #7

                I tried, it is throwing object ref not set an instance of an object exception. Are you sure :) Ram

                _ 1 Reply Last reply
                0
                • R Ramasubramaniam

                  I tried, it is throwing object ref not set an instance of an object exception. Are you sure :) Ram

                  _ Offline
                  _ Offline
                  _AK_
                  wrote on last edited by
                  #8

                  That is what you need to check. If you will use the session variable without assigning it then it will thow the exception exactly what you got. So prior to use that you have to check whether that is available or not like this: if(Session["x"]!=null) { //do your work. } here x is the variable name you change it with yours one. :)

                  Best Regards, Apurva Kaushal

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups