0

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();
    }
}

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much @Guffa. now can we treat it as simple Session variable?
@RaniaUmair: It's the same collection as you access using the Session property in the Page class. It's just a shortcut to the collection in the http context object.
1

Anyway, you can shorten your code using the following trick:

chkdt.Parameters.Add("@userName", SqlDbType.VarChar, 32).Value = username;
chkdt.Parameters.Add("@password", SqlDbType.VarChar, 250).Value = password;
chkdt.Parameters.Add("@location", SqlDbType.VarChar, 50).Value = locat;

And don't read a data reader twice:

DateTime? dt = reader["ExpiryDate"] as DateTime?; // if column has DateTime-compatible type
if (dt.HasValue)
{
}
else
{
}

And close data reader. Even better wrap everything in a using block(s):

using (SqlConnection conn = ...)
using (SqlCommand chkdt = ...)
{
   ...
   using (SqlDataReder reader = ...)
   {
      ...
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.