0

as I was trying to test/debug my applications by using QueryString as data to evaluate .

I was trying to implement same methods via session variables

in my other post regarding QueryStrings I was searching for a way to manipulate the QueryString via helper method ..

now I was trying to move on and triyng same thing to Session Variables using same method as with QueryStrings

code is

    public static class Sesion
    {
    public sealed class Act
    {
    public const string edit = "edit", add = "add", remove = "remove", replace = "replace";
    }

            public static void Modify(string action, string New_Value, string CurSes_ParamName, string NewSession_paramName = null, bool redirectWithNewQuerySettings = false)
            {

                #region <<=========== reflect to readonly & set QueriString ReadOnly - false ============>>



                //HttpContext.Current.Request.QueryString["qs_key"]; 

                // reflect to readonly property 
                PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

                // make collection editable 
                isReadOnly.SetValue(HttpContext.Current.Session, false, null);

                #endregion
                switch (action)
                {
                    case Act.remove:
                        if (itsNotAnEmptySession())
                            HttpContext.Current.Session.Remove(CurSes_ParamName);
                        break;
                    case Act.replace:
                        HttHttpContext.Current.Session.Remove(CurSes_ParamName);
                        HttpContext.Current.Session.Add(NewSession_paramName , New_Value);
                        break;
                    case Act.edit:
                        HttpContext.Current.Session.Set(CurSes_ParamName, New_Value);
                        break;

                    case Act.add:
                        HttpContext.Current.Session.Add(NewSession_paramName , New_Value);
                        break;

                }



                isReadOnly.SetValue(HttpContext.Current.Session, true, null);

            }
        }
}

the error i get on line that tries to set value of read only to false : as i am still not yet experienced enough with .net i couldn't understand where did i go wrong or ...is it totally impossible to implement that on session variables.

Object does not match target type.

trying it on QueryString and working with this method is fine as in this line of code:

isReadOnly.SetValue(HttpContext.Current.Request.QueryString, false, null);
Community
  • 1
  • 1
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76

1 Answers1

2

You can't just do the same with sessions :

First, Http.Current.Session is not a NameValueCollection (like QueryString), but an HttpSessionState (which explains your Object does not match target type message)

Then, HttpSessionState has an IsReadonly property, but... it's readonly (you can't set it). When the IsReadonly property from NameValueCollection is settable.

So... No way (at least this way).

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • thanks for the explanation . i tried just `Session.Add(param, value)` which works fine no problem adding to collection , although wanted to find out on editing a value , remove item from collection , will it be possible only remove first , add new(same parName), with new value ? – LoneXcoder Oct 05 '12 at 19:36
  • i think i have found the perfect solution in Application (state variables) any drawbacks ? it allows what ever you need in first glance – LoneXcoder Oct 05 '12 at 19:58