Storing session in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Astha
    New Member
    • Aug 2007
    • 6

    Storing session in array

    Hello ..I am new to asp.net .I have this problem of storing session values in a variable as an array.That is ..an array should contain the session values.How am i to do this in C#?and how can I retrieve them as well?

    Thanks in advance!
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by Astha
    Hello ..I am new to asp.net .I have this problem of storing session values in a variable as an array.That is ..an array should contain the session values.How am i to do this in C#?and how can I retrieve them as well?

    Thanks in advance!
    Could you please define the structure of your variable / class ?

    Comment

    • Astha
      New Member
      • Aug 2007
      • 6

      #3
      Hey ,thanks ...this is my piece of code..


      uname = Session["sessionnam e"].ToString();

      taskcd = new string[10];

      viewable = new string[10];

      editable = new string[10];

      deletable = new string[10];

      addable = new string[10];

      string sql = "select view_flag,edit_ flag,delete_fla g,add_flag,task _cd from Task_user where name='" + uname + "' ";

      cmd = new SqlCommand(sql, co.oConn);
      rd = cmd.ExecuteRead er();

      while (rd.Read())
      {

      taskcd[i] = rd["task_cd"].ToString();
      Session.Add("ta skcode", taskcd[i]);

      viewable[i] = rd["view_flag"].ToString();
      Session.Add("vi ewable", viewable[i]);

      editable[i] = rd["edit_flag"].ToString();
      Session.Add("ed itable", editable[i]);


      deletable[i] = rd["delete_fla g"].ToString();
      Session.Add("de letable", deletable[i]);


      addable[i] = rd["add_flag"].ToString();
      Session.Add("ad dable", addable[i]);

      i = i + 1;
      }

      rd.Close();



      this is the way I have been adding the session values to the array.But there must be some other better methods.Here,th ere are 6 task codes for 1 user.Each task code has all the view,add,delete and edit flags.What I am supposed to do is redirect the user to the page(or task),according to the privilege he has.Like some might have only previlige to view and not to edit,delete or add and so on.So,the main problem is to find this out and to redirect to the correct page.pls help.

      Thanks!

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Hi
        If i were you and was to stick to keeping all the code as strings
        i wud create a class and put the username, along with the 6 different values in it.
        If you want to club up all the strings as one object, its good to use ahash table or a dictionary to do it.

        Code:
        public class userSession
        {
           public string uname;
           public Dictionary<string, string> perms;
        }
        hope you do know how to use Dictionary....t here is lots of help already available if you may google.

        but looking at what you need, u might keep those 6 flags as booleans in the class i created above instead of a dictionary.

        Once u create an object of the class....store it into ure Session.

        cheers

        Comment

        Working...