How to get values from a session and download to variables?

1

I made this class:

public class ConexaoParametrosTFV
{
    public ConexaoParametrosTFV()
    {
        if ((SessaoUtil.Recuperar("ConexaoTFV") == null))
        {
            AgaxturCmsEntities db = new AgaxturCmsEntities();

            var resultado = (from a in db.TbClientes
                             where a.CdCliente == 1 && a.Ativo == "S"
                             select new {
                                 a.CultureTripoint,
                                 a.LoginTripoint,
                                 a.SenhaTripoint,
                                 a.SalesChannelTripoint,
                                 a.DepartmentIdTripoint,
                                 a.EntityIdTripoint }).First();

            SessaoUtil.SalvarSession("ConexaoTFV", "resultado");
        }
    }
}

I need now in another class, get everything I have loaded in the session and unload in six variables. How do I do this?

    
asked by anonymous 04.04.2014 / 17:58

3 answers

1

First you need to fix this line in your code:

//SessaoUtil.SalvarSession("ConexaoTFV", "resultado");
SessaoUtil.SalvarSession("ConexaoTFV", resultado);

Then recover with your class, if you are OK you will be able to do this:

var resultado = SessaoUtil.Recuperar("ConexaoTFV") as dynamic;
string login = resultado.LoginTripoint;

Do this for the rest of your query properties.

    
04.04.2014 / 20:19
0

Try to do so.

string teste = HttpContext.Current.Session["NOME_SESSION"];
    
04.04.2014 / 18:59
0

I did not realize, but thanks to @Roger's comment, I realized that you are using anonymous class objects.

Unable to read the properties of an anonymous class outside the method that created it. This happens because the definition of the anonymous class gets lost in the method that created it.

In these cases it is recommended to create a class with properties to represent the object. The anonymous class is not a good alternative in this case, where the object is used by more than one method.

Create a class to represent the object like this:

class MinhaClasse
{
     public string CultureTripoint { get; set; }
     public string LoginTripoint { get; set; }
     public string SenhaTripoint { get; set; }
     public string SalesChannelTripoint { get; set; }
     public string DepartmentIdTripoint { get; set; }
     public string EntityIdTripoint { get; set; }
}

And in your query do so:

       var resultado = (from a in db.TbClientes
                         where a.CdCliente == 1 && a.Ativo == "S"
                         select new MinhaClasse {
                             CultureTripoint = a.CultureTripoint,
                             LoginTripoint = a.LoginTripoint,
                             SenhaTripoint = a.SenhaTripoint,
                             SalesChannelTripoint = a.SalesChannelTripoint,
                             DepartmentIdTripoint = a.DepartmentIdTripoint,
                             EntityIdTripoint = a.EntityIdTripoint }).First();

        SessaoUtil.SalvarSession("ConexaoTFV", "resultado");

Then to retrieve values:

var meuObjeto = (MinhaClasse)SessaoUtil.Recuperar("ConexaoTFV");
    
04.04.2014 / 19:53