Murat Aşık Murat Aşık - 3 months ago 20
ASP.NET (C#) Question

C# Checking variables whether defined or not

I just started learning asp.net and c#.

Let say I have a session like this:

Session.Add("username","me");


At a later section, I wanna learn whether that session has been defined. How can i do that ? I tried to do this :

if(Session["username"]){
// some code
}


but of course it didn't work.

Answer

Try this code

        if (Session["username"] != null)
        {

        }
Comments