【发布时间】:2020-04-01 06:42:32
【问题描述】:
我在 MasterPage 中有一个名为 MailContainer 的静态类。
在 MailContainer 类中,我为 get /set 定义了如下属性。
public static class MailContainer
{
public static string TheObjectPropertyEmail
{
get
{
return HttpContext.Current.Session["TheObjectPropertyEmail"].ToString();
}
set
{
HttpContext.Current.Session["TheObjectPropertyEmail"] = value;
}
}
}
当我尝试使用 MasterPage 在 Default.aspx.cs 上分配如下值时。
MailContainer.TheObjectPropertyEmail = reader["Email"].ToString();
它抛出以下异常。
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();
}
}
}
}
【问题讨论】: