ASP NET会话抛出.ToString()错误

Cai*_*s89 1 .net c# asp.net webforms

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

其他问题是在IE9中,有时候不会保存我的Session以将Idiom更改为其他页面.在Chrome中一切都很好!

错误

在此输入图像描述

下面是我的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";
}
Run Code Online (Sandbox Code Playgroud)

chr*_*dev 9

更换

if (Session["idioma"].ToString() != null)
Run Code Online (Sandbox Code Playgroud)

if (Session["idioma"] != null)
Run Code Online (Sandbox Code Playgroud)

如果session objectNULL,则调用.ToString()将抛出错误.