0

I am using C#.

I have got below format values in my SESSION variable ["FROMDATA"], I am using DICTIONARY to store the FORM Posted Data. Please see the related question.

Below are the some values in my SESSION Variable.

1) key - "skywardsNumber" value-"99999039t"
2) key - "password" value-"a2222222"
3) key - "ctl00$MainContent$ctl22$FlightSchedules1$ddlDepartureAirport-suggest" value-"London"
4) key - "ctl00$MainContent$ctl22$ctl07$txtPromoCode" value-"AEEGIT9"
.
.
....so on

Now I want to create a CLASS with METHOD in it, in which I will just pass the "KEY" and it will first check it for NULL OR EMPTY and then it will return its value from the SESSION Variable ["FROMDATA"].

Please suggest using C#.

3
  • Questions are not judged by bold text Commented Jan 19, 2011 at 5:33
  • I just thought it would be easy for reading and I was just highlighting the points. Commented Jan 19, 2011 at 5:34
  • 1
    You are not correct in questioning. Either remove bold or mark specific correct answer. Do not require specific responce to session processing code ("FROMDATA"), or implicitly ask for generic soution, please. Commented Jan 19, 2011 at 6:05

2 Answers 2

5

Try this,

public class Test
{
    public static string GetValue(string key)
    {
        string value = string.Empty;
        if (HttpContext.Current.Session["FROMDATA"] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session["FROMDATA"];
            if(form.ContainsKey(key))
                value = form[key];
        }
        return value;
    }
}

EDIT:

 public static string GetValue(string sessionkey,string key)
    {
        string value = string.Empty;
        if (HttpContext.Current.Session[sessionkey] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[sessionkey];
            if(form.ContainsKey(key))
                value = form[key];
        }
        return value;
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I am looking to make more generic, in which I can pass the Session["FROMDATA"] also, so this class can be used for other SESSION variable, and also I need to check the IsNullOrEmpty for the entered KEY not for whole SESSION variable.
Yeah! you can pass session key along with dictionary key.
You mean it should be normal passing as we are passing "key" in above code. Please suggest
@MKS - I've modified my post.
0

Try this: you'll have to tweak it a bit for it to complie. But you'll get the basic idea

public class Session
    {        

        private const string _skyWardsNumber = "skyWardsNumber";
        // Add other keys here

        public string SkyWardsNumber
        {
            get
            {
                object str = (ReadFromDictionary(_skyWardsNumber);
                if (str != null)
                {
                    return (string) str;
                }
                else
                {
                    return string.Empty;
                }

            }
            set
            {
                WriteToDictionary(_skyWardsNumber, value);
            }
        }

        public object ReadFromDictionary(string key)
        {
            IDictionary dictionary = (ReadFromContext("Dictionary") as IDictionary);
            if (dictionary != null && dictionary.ContainsKey(key))
            {
               return dictionary[key];
            }
            else
            {
               return null;
            }
        }

        public object WriteFromDictionary(string key, object value)
        {
            IDictionary dictionary = (ReadFromContext("Dictionary") as IDictionary);

            if(dictionary == null)
                 WriteToContext("Dictionary", new Dictionary<string, string>())

            dictionary = (ReadFromContext("Dictionary") as IDictionary);

            if (dictionary.ContainsKey(key))
            {
               dictionary[key] = value;
            }
            else
            {
               dictionary.Add(//add new keyvaluepair.);
            }
        }

        private static void WriteToContext(string key, object value)
        {
            HttpContext.Current.Session[key] = value;
        }


        private static object ReadFromContext(string key)
        {
            if(HttpContext.Current.Session[key] != null)
                return HttpContext.Current.Session[key] as object;

             return null;
        }

    }

2 Comments

Thanks DiVi, but where we are going to use Session["FROMDATA"] it should be generic also
You can rename "Dictionary" to "FROMDATA". The basic idea is that the session will have a dictionary, which will have key value pairs. Then you can add properties for the key value pairs on your session class eg. SkyWardNumber, which reads its value from Dictionary, which in turn reads its value from the session. I hope that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.