How to take values from a Session and download them into variables?

Asked

Viewed 3,432 times

1

I made that 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, pick up everything you’ve clicked on Session and download into six variables.
How do I do it?

  • SessaoUtil.SalvarSession("ConexaoTFV", "resultado"); seems to be wrong, "resultado" is a string, and does not reference the variable resultado.

  • I didn’t even notice it

  • Saves the object in Session, then arrow each variable with the value of each object property.

  • That’s my question. How do I do it? That’s the reason for my post.

  • var recovering = Sessaoutil.Recover("Conexaoftv"); var1 = recovering.var1; var2 = recovering.var2;

  • Doesn’t work that way

  • instead of using var, use Dynamic, when you use var it creates an Anonymous and Voce will not know at the time of recovering what is the type of your object. Use Dynamic when capturing Tbclientes result

  • @Roger: now that OP is using anonymous class objects. Still, I wouldn’t recommend using dynamic, rather a class to represent the object.

  • @Miguelangelo I agree with you 100% but this would set a precedent so that he had to declare a new class for each different query in the Views.

  • Yes, this is what happens for example with Viewmodel and Domainmodel classes. Each has a representative class, and this makes the project more consistent.

  • @Roger: Even if I disagree with you, I could add an answer to your solution... it’s a valid solution. = D

  • @Miguelangelo, the Uri solution does what I suggested, it should work. I agree that the ideal is to use a Viewmodel for any and all MVC views but since the little friend is not using I usually be pragmatic in giving the solution in line with his head. :)

Show 7 more comments

3 answers

1

First you need to fix that 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.

0

Try to do so.

string teste = HttpContext.Current.Session["NOME_SESSION"];
  • So I bring the data for the variable. var test = Sessaoutil.Recover("Conexaotfv"); What I can’t do is to assign the variables, the fields, for example: Culture = test.Culturetripoint and so on. He doesn’t recognize the data in the variable: Culturetripoint, I mean, I can’t catch this guy inside the var or Session.

  • What you have to do is cast on Session and convert it to the same object you are recording. ex: if you want a property of the class Conexaotfv: código Conexaotfv con = new Conexaotfv(); Httpcontext.Current.Session["NOME_SESSION"] = con; con.Culturetripoint = "Chosen Culture". To read the Session: Connecttfv test = (Connecttfv)Httpcontext.Current.Session["NOME_SESSION"]; test.Culturetripoint

  • or try string test =((Conexaotfv)Httpcontext.Current.Session["NOME_SSSION"]). Culteuretripoint;

  • I created a class with get and set and loaded its attributes in the constructor where I create my Session and loaded Session with these values. On the other side, I instate the class and now I can assign the values of the attributes to the variables. It worked. Thank you all. Ah! Yes, I did the cast that Marco suggested, of course, because without it, it wouldn’t really work. Thus: var _conexao = (Conexaotfv)Sessaoutil.Recover("Conexaotfv"); Conexaotfv is my class POCO.

0

I hadn’t noticed, but thanks to @Roger’s comment, I noticed you’re using anonymous class objects.

It is not possible to read the properties of an anonymous class outside the method that created it. This happens because the definition of the anonymous class is 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 recover the values:

var meuObjeto = (MinhaClasse)SessaoUtil.Recuperar("ConexaoTFV");
  • That already did and worked. A question to the masters. I have in the class builder my Ssion. I always prompt the class to generate the constructor. Then I create a var of the class type and assign the values of Session to this var. Is that correct? Or instead of two lines, do I only need one? Do I really have to instantiate the class because of the constructor or just invoke the Session that will be done? I do so:Connectparametrostfvconnection = new Connectparametrostfv(); var _connectv = (Connecttfv)Sessaoutil.Recover("Connecttfv");

  • If the object is stored in Session, then there is no need to create another. This line is enough: var _conexao = (ConexaoTFV)SessaoUtil.Recuperar("ConexaoTFV");

Browser other questions tagged

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