Search code examples
c#asp.netsessioncollectionsordereddictionary

The OrderedDictionary is read only and cannot be modified


Q:

When i try the following code :

 ((IOrderedDictionary)Session["keys"])[2] = objToUpdate.Note_title;
 ((IOrderedDictionary)Session["keys"])[3] = objToUpdate.Iscourse;

It throws an exception:

The OrderedDictionary is read only and cannot be modified.


My question:

  • Why just read only?
  • How to set my key in the session,fixing my problem?

EDIT:

I fill the IOrderedDictionary through this:

IOrderedDictionary keys = gv_courses.DataKeys[index].Values;

Session["Keys"] = keys;

Solution

  • I reach what i want to do ,thanks for all replies ..

                    IOrderedDictionary keys = gv_courses.DataKeys[index].Values;
                    string[] keysTemp = new string[4];
                    keysTemp[0] = keys[0].ToString();
                    keysTemp[1] = keys[1].ToString();
                    keysTemp[2] = keys[2].ToString();
                    keysTemp[3] = keys[3].ToString();
                    Session["KeysCourse"] = keysTemp;
    

    Then

     ((string[])Session["KeysCourse"])[2] = objToUpdate.Note_title;
     ((string[])Session["KeysCourse"])[3] = objToUpdate.Iscourse.ToString();