Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Passing Session variable into c#

Posted on 2011-03-23
8
389 Views
Last Modified: 2013-12-17
Hi,

I'm not very familiar with c# but I'm trying to pass a session variable into a c# page abd having problem,

I'm guessing it's something with the private code block

the variable sets in login

 Context.Session("aspnetforumUserNickName") = "Administrator"


and when I try to load the c# page I  get this error

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 112:                        {
Line 113:                            //username = Session["aspnetforumUserName"].ToString();
Line 114:                            username = Session["aspnetforumUserNickName"].ToString();
Line 115:
Line 116:                        }


any help would be appreciated





 
protected void Page_PreRender(object sender, EventArgs e)
        {
            cmd.Connection = cn;

            //if (lblVersion != null)
            //{
             //   string dllversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            //    lblVersion.Text = dllversion;
            //}

            bool integratedAuthEnabled = Utils.Settings.IntegratedAuthentication;
            //bool isForumPage = (Page is ForumPage);

            if (isForumPage)
            {
                if (CurrentUserID == 0) //not logged in
                {
                    //if (registerLink != null) registerLink.Visible = !integratedAuthEnabled;
                    if (logoutLink != null) logoutLink.Visible = false;
                    if (viewProfileLink != null) viewProfileLink.Visible = false;
                    if (loginTable != null) loginTable.Visible = !integratedAuthEnabled && ShowLoginTable;
                    if (curuserTable != null) curuserTable.Visible = false;
                    if (usersLink != null) usersLink.Visible = false;
                    if (aOpenId != null) aOpenId.Visible = Utils.Settings.EnableOpenId;
                }
                else //logged in
                {
                    //if (registerLink != null) registerLink.Visible = false;
                    //if (logoutLink != null) logoutLink.Visible = !integratedAuthEnabled;
                    if (loginTable != null) loginTable.Visible = false;
                    if (curuserTable != null) curuserTable.Visible = ShowLoginTable;
                    if (usersLink != null) usersLink.Visible = true;
                    //if (editProfileHeaderLink != null) editProfileHeaderLink.Visible = true;

                    if (viewProfileLink != null)
                    {
                        viewProfileLink.Visible = true;
                        string username;
                        //IF "integrated auth" is enabled
                        //AND it is windows-authenctication
                        //then lets remove tha domain name from "domain\user" username
                        if (Page.User is System.Security.Principal.WindowsPrincipal
                            && Utils.Settings.IntegratedAuthentication)
                        {
                            username = Session["aspnetforumUserName"].ToString();
                            username = username.Substring(username.IndexOf("\\") + 1);
                        }
                        else
                        {
                            //username = Session["aspnetforumUserName"].ToString();
                            username = Session["aspnetforumUserNickName"].ToString();

                        }
                        viewProfileLink.InnerHtml = username;
                    }

                    cn.Open();
                    int unreadPrivateMsgs = GetUnreadPersonalMessagesCount();
                    string avatarPath = GetCurrUserAvatarImagePath();
                    cn.Close();

                    if (imgAvatar != null) imgAvatar.Src = avatarPath;
                    if (spanNumMsgs != null)
                    {
                        spanNumMsgs.InnerHtml = unreadPrivateMsgs.ToString();
                        if (unreadPrivateMsgs > 0) spanNumMsgs.Style["font-weight"] = "bold";
                    }
                    if (spanNumUnreadThreads != null)
                    {
                        int updatedTopics = GetUpdatedThreadCount();
                        if (updatedTopics > 0)
                            spanNumUnreadThreads.InnerHtml = "(" + updatedTopics + ")";
                    }
                }
            }

            adminLink.Visible = isForumPage && IsAdministrator && adminLink != null;
            moderLink.Visible = isForumPage && IsModerator && moderLink != null;
        }

Open in new window

0
Comment
Question by:TheWebGuy38
  • 4
  • 3
8 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 35196356
check the condition like this

if ( Session["aspnetforumUserNickName"] !=  null )
                                                                                             {
                                                       username = Session["aspnetforumUserNickName"].ToString();


if its null means session isnot set properly                                       }
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35196370
Insert the data to session like this

Session["aspnetforumUserNickName"] = "Administrator"
0
 

Author Comment

by:TheWebGuy38
ID: 35196527
this is my vb code setting the session. I'm pretty sure it's correct

 Context.Session("aspnetforumUserID") = AspnetforumUserID
                        Context.Session("aspnetforumUserName") = Dr("UserName")
                        Context.Session("aspnetforumUserNickName") = "Administrator"

is there an issue going from vb to c?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:TheWebGuy38
ID: 35196546
Very strange.

I added this like you said

         username = Session["aspnetforumUserNickName"].ToString();
                            if ( Session["aspnetforumUserNickName"] !=  null )
                                                                                           
                                                      username = Session["aspnetforumUserNickName"].ToString();
                        }

and it passed the variable
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35198198
How do your VB code and C# code relate to each other...i.e. is the VB code an HttpHandler, and the C# code the website - or do you have two different websites?

I'm also pretty sure your VB code is correct, but I'm also pretty sure checking for null before attempting to read the session variable has no effect on the exception you've cited. Which means either A) It was always working (and was just a rare condition/glitch that threw the exception in the first place) or B) It's not working now (and your test worked because, perhaps, of a condition unique to the debugging environment).
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35204641
ok your Vb cod is right ?

is this are two differnt applications then I don't think you can share session like this

you can try to store the Session object in the database, that's easy to pass the Session object to other application
0
 

Author Comment

by:TheWebGuy38
ID: 35204657
It seems to be working now.

I coded my site in .vb, but then I purchased a forum app that I integrated into the site that is c#
I know, a little messy, but it works
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35204664
ok thats good
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question