Avatar of skillilea
skillilea
 asked on

Loop through a session Object

New to this...
asp.net, C# 4.0

I am trying to loop through an object that I have put into a session for debugging display purposes.


I used this...

        // set the user into a session either loggedin or current
        public static void SetUserInfo(UserInfo User, bool LoggedIn)
        {
            string uType = _currentUser;
            if (LoggedIn){ uType = _loggedInUser;}

            if (HttpContext.Current.Session[uType] != null)
                HttpContext.Current.Session[uType] = User;
            else
                HttpContext.Current.Session.Add(uType, User);
        }

        //return the current UserInfo
        public static UserInfo GetCurrentUser(bool LoggedIn)
        {
            string uType = _currentUser;
            if (LoggedIn) { uType = _loggedInUser; }

            return HttpContext.Current.Session[uType] as UserInfo;
        }


Now this is where I am stuck...

                StringBuilder _sb = new StringBuilder();

                _sb.Append("<hr>");
                _sb.Append("<b>Current Session Count</b>");
                _sb.Append("<hr>");
                _sb.Append("Session Count: ");
                _sb.Append(HttpContext.Current.Session.Count.ToString());
                _sb.Append("<hr>");
                _sb.Append("Logged In User: ");
                UserInfo u = UserInfoManager.GetCurrentUser(true);
                _sb.Append("Name: ");
                _sb.Append( u.UserName.ToString() );
                _sb.Append("<br>");

                //need to loop here

                _res = _sb.ToString();


I want to display each item...what is a better way?

thanks tons
ASP.NETC#

Avatar of undefined
Last Comment
Miguel Oz

8/22/2022 - Mon
kevinhigg

Sorry for my confusion, but could you please clarify your intention?  When you say display each item, you're looking to flush the markup in _sb (or _res) to the output buffer?  Or are you looping over something else?  If this is related to your StringBuilder content, you can likely use Response.Write().  If you're interested in looping over something else, you can check out a simple foreach example (and a little more detail) here, but not sure what your object may look like.  Kind regards.

http://www.dotnetperls.com/response-write
skillilea

ASKER
Thanks and sorry for the confusion...

Here is my current stringbuilder...

I am using this to test in the footer of the page as the users change.


I want to be able to take the OBJECT  UserInfo   and loop through it with a foreach

something like this

foreach( string item in UserInfo)
{
   name + value

}



           if (writeOut)
            {
                StringBuilder _sb = new StringBuilder();

                _sb.Append("<hr>");
                _sb.Append("<b>Current Session Count</b>");
                _sb.Append("<hr>");
                _sb.Append("Session Count: ");
                _sb.Append(HttpContext.Current.Session.Count.ToString());
                _sb.Append("<hr>");
                _sb.Append("<b>LoggedIn User</b><br> ");
                UserInfo u = UserInfoManager.GetCurrentUser(true);

                if (u != null)
                {
                    _sb.Append("Name: " + u.UserName.ToString() + "<br>");
                    _sb.Append("GUID: " + u.UserGUID.ToString() + "<br>");
                    _sb.Append("ID: " + u.UserID.ToString() + "<br>");
                    _sb.Append("Language: " + u.UserLanguageID.ToString() + "<br>");
                    _sb.Append("PeriodID: " + u.UserPeriodID.ToString() + "<br>");
                }
                _sb.Append("<hr>");
                _sb.Append("<b>Current User</b><br> ");
                UserInfo uC = UserInfoManager.GetCurrentUser(false);

                if (uC != null)
                {
                    _sb.Append("Name: " + uC.UserName.ToString() + "<br>");
                    _sb.Append("GUID: " + uC.UserGUID.ToString() + "<br>");
                    _sb.Append("ID: " + uC.UserID.ToString() + "<br>");
                    _sb.Append("Language: " + u.UserLanguageID.ToString() + "<br>");
                    _sb.Append("PeriodID: " + u.UserPeriodID.ToString() + "<br>");
                }

                _res = _sb.ToString();

            }
            return _res;
        }


So I don't have to keep updating this display with each new item everytime I change the object.


Is there a way to make it a generic list and then just loop through it or bind it to something.

thanks

sk
ASKER CERTIFIED SOLUTION
Miguel Oz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy