Session variable always returns Null Asp.Net

Asked

Viewed 128 times

1

Business Class:

public class Empresa
{
    public long empresaId { get; set; }
    public string nomeEmpresa { get; set; }

    [Display(Name = "login")]
    [Required(ErrorMessage = "Informe Seu Login", AllowEmptyStrings = false)]
    public string loginEmpresa { get; set; }

    [Display(Name = "Senha")]
    [Required(ErrorMessage = "Informe a senha do usuário", AllowEmptyStrings = false)]
    [DataType(DataType.Password)]

    public string senhaEmpresa { get; set; }


    public ICollection<Projeto> Projetos{get;set;}
}

Action to Login:

public ActionResult LoginEmpresa(string x, string y)
{
    if (ModelState.IsValid) //verifica se é válido
    {
        //var v = context.Empresas.Where(a => a.loginEmpresa.Equals(empresa.loginEmpresa) && a.senhaEmpresa.Equals(empresa.senhaEmpresa)).First();


        string login = empresaServico.ObterEmpresaPorLogin(x).ToString();
        string senha = empresaServico.ObterEmpresaPorSenha(y).ToString();

        if ((login != null) && (senha != null))
        {
            //Session["empresaLogadaID"] = .ToString();
            Session["nomeEmpresaLogada"] = login.ToString();
            return RedirectToAction("/Projeto/Index");
        }

        else return RedirectToAction("/Projeto/Index");
}

View where she redirects:

@{if (Session["nomeEmpresaLogada"] != null)
{
    <h2>Bem-vindo(a),@Session["nomeEmpresaLogada"].</h2>

    <div>
        Relação de projetos

    </div>
  • Check that your login variable actually has some value to be set in friend Session.

2 answers

0

  • I believe that if Session returns Null will not help the extensive Tostring method()

  • A Session ta retornando null

0

When I use it I’m doing it this way:

HttpContext.Session.SetString("Nome", variavel);

and to read the Session:

HttpContext.Session.GetString("Nome")

Have you checked if it gets any value and after that it gets null ?

  • She is returning null

  • 1

    @Nicésiojunior On this line Session["nomeEmpresaLogada"] = login.ToString(); when you put a breakpoint the login.ToString() has some value ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.