c# - ASP NET session 使用 .ToString() 抛出错误

标签 c# asp.net .net webforms

我在 IE9 的 ASp.NEt c# 项目中使用 Session 时遇到问题。 有时会出现错误: “对象引用未设置为对象的实例”

另一个问题是在 IE9 中,有时不保存我的 Session 以将 Idiom 更改为其他页面。 在 Chrome 中一切正常!

Error

enter image description here

下面是我的 Page_Load 和 CarregaGrid()。此问题有时出现,并非所有时间出现,并且在任何页面中都没有出现在所有页面中或仅出现在一个特定页面中。

    public void CarregaGrid()
{
    var listByGroupM = new ManageUsers().ConsultUsersGroupM();
    if (listByGroupM != null)
    {
        this.GridView1.DataSource = listByGroupM;
        if (listByGroupM.Count != 0)
        {
            this.GridView1.DataBind();
            GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
    }

    if (divModify.Visible == true)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            string idioma = CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString();

            if (Session["idioma"].ToString() != null)
            {
                idioma = Session["idioma"].ToString();
            }

            Idioma.MudaCultura(idioma);

            Button btnDelete = (Button)row.FindControl("btnDelete");
            btnDelete.Text = Idioma.RetornaMensagem("btnDelete");

            string UserName = row.Cells[1].Text;
            PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "x.com", "x", "xxx");
            UserPrincipal insUserPrincipal = UserPrincipal.FindByIdentity(insPrincipalContext, UserName);

            if (insUserPrincipal == null)
            {
                row.Cells[2].Text = "";
                row.Cells[3].Text = "";
            }

            else
            {
                string Email = insUserPrincipal.EmailAddress;
                row.Cells[2].Text = Email;
                string DisplayName = insUserPrincipal.DisplayName;
                row.Cells[3].Text = DisplayName;
            }
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string idioma = CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToString();

        if (Session["idioma"].ToString() != null)
        {
            idioma = Session["idioma"].ToString();
        }

        Idioma.MudaCultura(idioma);
        Label1.Text = Idioma.RetornaMensagem("lblUserAdd");
        CarregaGrid();
    }
}

protected void pt_OnChange(object sender, EventArgs e)
{
    Idioma.MudaCultura("pt");
    Label1.Text = Idioma.RetornaMensagem("lblUserAdd");
    CarregaGrid();
    Session["idioma"] = "pt";
}

protected void en_OnChange(object sender, EventArgs e)
{
    Idioma.MudaCultura("en");
    Label1.Text = Idioma.RetornaMensagem("lblUserAdd");
    CarregaGrid();
    Session["idioma"] = "en";
}

protected void es_OnChange(object sender, EventArgs e)
{
    Idioma.MudaCultura("es");
    Label1.Text = Idioma.RetornaMensagem("lblUserAdd");
    CarregaGrid();
    Session["idioma"] = "es";
}

最佳答案

替换

if (Session["idioma"].ToString() != null)

if (Session["idioma"] != null)

如果 session 对象NULL,那么调用.ToString()将抛出一个错误

关于c# - ASP NET session 使用 .ToString() 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18255497/

相关文章:

asp.net - 如果没有存储过程,如何在 ASP.NET 中对结果集进行分页?

asp.net - 确定 ASP.Net 是否正确注册

c# - 为什么.NET 中没有 IDateTimeProvider 而 DateTime 有 Now getter?

.net - 何时使用 CultureInfo.GetCultureInfo(String) 或 CultureInfo.CreateSpecificCulture(String)

c# - .NET - neo4jclient - 从密码查询中检索结果

c# - 异步等待导致死锁

C# WPF 线程

c# - 管理 Application Insights Cookie

c# - MVVM 模式 : an intermediate View between Command binding and ViewModel execute

c# - Rabbitmq:exchange->routingkey->queue命名结构