Search code examples
c#asp.net.netsession.net-2.0

Session variable generate error


i am trying to use session but i get error:

The name 'Session' does not exist in the current context

what i am doing wrong i am using n-tier and in this page there is no page load function. Session is having link with page_load?

public bool CheckDate(ArrayList roles, string username, string password, string locat)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONN"].ToString());
    SqlCommand chkdt = new SqlCommand("AccountRoles_GetDateForID", conn);
    chkdt.CommandType = CommandType.StoredProcedure;
    chkdt.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar, 32));
    chkdt.Parameters["@userName"].Value = username;
    chkdt.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 250));
    chkdt.Parameters["@password"].Value = password;
    chkdt.Parameters.Add(new SqlParameter("@location", SqlDbType.VarChar, 50));
    chkdt.Parameters["@location"].Value = locat;
    conn.Open();
    try
    {
        DateTime ddt = new DateTime();
        DateTime tdd = DateTime.Parse(DateTime.Now.ToShortDateString());
        SqlDataReader reader = chkdt.ExecuteReader();
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                if (reader["ExpiryDate"].ToString() == "")
                {
                }
                else
                {
                    ddt = DateTime.Parse(reader["ExpiryDate"].ToString());
                }
            }
        }
        TimeSpan ts = ddt.Subtract(tdd);
        day = ts.Days.ToString();
        Session["days"] = day;
        if (tdd.Equals(ddt))
        {               
            return true;
        }
        else
        {
            return false;
        }
    }
    finally
    {
        conn.Close();
        chkdt.Dispose();
    }
}

Solution

  • If your method is not in a class that inherits from Page, the Session property isn't inherited.

    Use the Current property of the HttpContext class to access the current http context where the Session collection is:

    HttpContext.Current.Session["days"] = day;