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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to check session is created or not?

How to check session is created or not?

Scheduled Pinned Locked Moved ASP.NET
csharphelptutorialquestion
6 Posts 5 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.
  • B Offline
    B Offline
    BalasubramanianK
    wrote on last edited by
    #1

    Hi, How to check whether the session is created or not in c#? I am using like If (Session("Name") == null), but it is showing error named, object reference is not set to an object. tnx in adv.

    Balasubramanian K.

    E 1 Reply Last reply
    0
    • B BalasubramanianK

      Hi, How to check whether the session is created or not in c#? I am using like If (Session("Name") == null), but it is showing error named, object reference is not set to an object. tnx in adv.

      Balasubramanian K.

      E Offline
      E Offline
      eyeseetee
      wrote on last edited by
      #2

      if (Session["Name"] != null) { stName = Session["Name"].ToString(); } else { stName= ""; } something like the above We are not a Code Charity

      B 1 Reply Last reply
      0
      • E eyeseetee

        if (Session["Name"] != null) { stName = Session["Name"].ToString(); } else { stName= ""; } something like the above We are not a Code Charity

        B Offline
        B Offline
        BalasubramanianK
        wrote on last edited by
        #3

        tnx. It is working fine. If it is working with !=, why it is not working with '=='. Why it throws error?

        Balasubramanian K.

        S T N 3 Replies Last reply
        0
        • B BalasubramanianK

          tnx. It is working fine. If it is working with !=, why it is not working with '=='. Why it throws error?

          Balasubramanian K.

          S Offline
          S Offline
          Sherin Iranimose
          wrote on last edited by
          #4

          It should work! Can you copy and paste your coding.

          EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

          1 Reply Last reply
          0
          • B BalasubramanianK

            tnx. It is working fine. If it is working with !=, why it is not working with '=='. Why it throws error?

            Balasubramanian K.

            T Offline
            T Offline
            tina newcoder
            wrote on last edited by
            #5

            BalasubramanianK wrote:

            why it is not working with '=='

            if you are comparing with a string, like if(nameStr==Session["name"] ) ; then, for that you need to first use ToString(). like this ... if(nameStr==Session["name"].ToString()) ; Is these you were looking for ... ?

            1 Reply Last reply
            0
            • B BalasubramanianK

              tnx. It is working fine. If it is working with !=, why it is not working with '=='. Why it throws error?

              Balasubramanian K.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              BalasubramanianK wrote:

              If it is working with !=, why it is not working with '=='. Why it throws error?

              Do you know what != and == means ? First one is checking inequality and second is equality checking. So if you write

              if(Session["name"] == NULL)
              string name = Session["name"].ToString();

              will throw error as you are trying to access a method in a NULL referenced object. If you change the code to

              if(Session["name"] != NULL)
              string name = Session["name"].ToString();

              , you are accessing ToString() only when Session["name"] exist. Hope it helps

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

              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