Reputation: 2015
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();
}
}
Upvotes: 0
Views: 150
Reputation: 100366
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 = ...)
{
...
}
}
Upvotes: 1
Reputation: 700860
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;
Upvotes: 2