1

我在 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";
}
4

2 回答 2

9

代替

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

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

如果session objectNULL,则调用.ToString()将引发错误

于 2013-08-15T15:03:43.490 回答
4

改变:

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

至:

if (Session["idioma"] != null)
于 2013-08-15T15:04:08.353 回答