Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#set和get中HttpContext.Current.Session中的会话值_C#_Session_Get_Set - Fatal编程技术网

C#set和get中HttpContext.Current.Session中的会话值

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"]

我在母版页中有一个名为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;
        }
     } 
}
当我尝试在母版页的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相关的项目中可用,如

  • asp.net的WebForms
  • WebApi
  • MVC
  • 现在取决于生命周期,HttpContext是否初始化。我们需要了解更多关于您的具体示例。当您致电并设置时,请让我们为您提供帮助

    干杯

    检查这里。此HttpContext仅在与web相关的项目中可用,如

  • asp.net的WebForms
  • WebApi
  • MVC
  • 现在取决于生命周期,HttpContext是否初始化。我们需要了解更多关于您的具体示例。当您致电并设置时,请让我们为您提供帮助


    干杯

    谢谢我已经编辑了我的第一个问题,你能帮我吗?你在哪里做作业?在构造器中?该赋值位于带有母版页的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();
                }
            }
        }
    }