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

时间:2013-08-15 15:00:03

标签: 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";
}

2 个答案:

答案 0 :(得分:9)

替换

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

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

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

答案 1 :(得分:4)

变化:

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

为:

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