comparing session variable value to a string

◇◆丶佛笑我妖孽 提交于 2019-12-05 09:43:52

Cast the object type value to a string

((string)Session["loggedInUserType"]) == "Administrator"

Try

if(Convert.ToString(Session["loggedInUserType"]) == "Administrator) ...

Try this:

if (Session["loggedInUserType"].ToString().Trim()
        .Equals("Administrator", StringComparison.InvariantCultureIgnoreCase))

Are you sure that all whitespace has been trimmed from the end of the Session["loggedInUserType"]?

if (Session["loggedInUserType"].ToString() == "Administrator")

The Session collection returns values of type Object, so when you compare that to a string you will be comparing the values of the object references, not comparing the string values.

Cast the object reference to string:

if (((string)Session["loggedInUserType"]) == "Administrator")
SarahB

You can do this :

string session = (string)Session["loggedInUserType"]

if (session == "Administrator")

or your Session can be in a specific class with getters.

if(Convert.ToString(Session["loggedInUserType"]) == "Administrator)

In this way, no need to check null value becuase Convert.ToString handle Null value return "" empty string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!