C#set和get中HttpContext.Current.Session中的会话值
我在母版页中有一个名为MailContainer的静态类 在MailContainer类中,我定义了get/set的属性,如下所示C#set和get中HttpContext.Current.Session中的会话值,c#,session,get,set,C#,Session,Get,Set,我在母版页中有一个名为MailContainer的静态类 在MailContainer类中,我定义了get/set的属性,如下所示 public static class MailContainer { public static string TheObjectPropertyEmail { get { return HttpContext.Current.Session["TheObjectPropertyEmail"]
public static class MailContainer
{
public static string TheObjectPropertyEmail
{
get
{
return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();
}
set
{
HttpContext.Current.Session["TheObjectPropertyEmail"] = value;
}
}
}
当我尝试在母版页的Default.aspx.cs上指定如下值时
它抛出以下异常
System.NullReferenceException:对象引用未设置为实例
指一个物体
在这一行:
return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();
我该如何解决这个问题
编辑#01
public void Aut()
{
sql = @String.Format(" SELECT * FROM doTable ");
sql += String.Format(" WHERE ");
sql += String.Format(" UPPER(user) IN (?); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
if (username != null)
{
command.Parameters.AddWithValue("param1", username.ToString().ToUpper());
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
MailContainer.TheObjectPropertyEmail = reader["Email"].ToString();
}
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
在这里检查。此HttpContext仅在与web相关的项目中可用,如
干杯 谢谢我已经编辑了我的第一个问题,你能帮我吗?你在哪里做作业?在构造器中?该赋值位于带有母版页的Default.aspx.cs上。请参见我的第一个问题中的Edit#01谢谢我已经编辑了我的第一个问题,你能帮我吗?你在哪里做作业?在构造器中?该赋值位于带有母版页的Default.aspx.cs上。请参见我第一个问题中的编辑#01
public void Aut()
{
sql = @String.Format(" SELECT * FROM doTable ");
sql += String.Format(" WHERE ");
sql += String.Format(" UPPER(user) IN (?); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
if (username != null)
{
command.Parameters.AddWithValue("param1", username.ToString().ToUpper());
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
MailContainer.TheObjectPropertyEmail = reader["Email"].ToString();
}
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}