Problems Converting Session to Class in C#

Asked

Viewed 111 times

1

I have the following variable

List<MyFile> xmlDisponivel = new List<MyFile>();

This variable is a list of the following class

public class MyFile
{
    public string FileName { get; set; }
    public string FilePath { get; set; }
}

In a process I save the list that assigns this variable in a Session

Session.Add("XmlDisponivel", xmlDisponivel);

In another process when I will try to take this information from Ssion and move to my type variable List<MyFile> always comes null even if you have information on the list.

As print below, I have a list of 400 files, but when I try to convert from Session to my variable, it always comes null.

Watch1

How to solve, some idea?

  • And how are you doing the Cast of Session for List? Edita your question for us to see :)

  • 2

    var lista = (List<MyFile>) Session["XmlDisponivel"]; ?

  • @Onosendai shows in print that the conversion does not work.

  • @jbueno obj as type != (type) obj. The operator as only considers reference and Boxing/Unboxing.

  • Instead of setting like this: Session.Add("XmlDisponivel", xmlDisponivel);, set like this: Session["XmlDisponivel"] = xmlDisponivel also doesn’t work?

  • @Onosendai It worked perfectly, obg!! Obg a tds who tried to help!

  • 2

    @Wellitonmeneguim Always a pleasure to help! A suggestion - create an answer describing your solution, and mark it as such. So you help future site visitors. =)

Show 2 more comments

1 answer

-1

var lista = (List<MyFile>) Session["XmlDisponivel"];
  • 2

    Renato, I recommend putting an explanation to make the answer clearer and the user understand why he should use the solution presented.

Browser other questions tagged

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