0

I am trying to pass a users reputation(just like SO) to the masterpage when the user logs in like below :

Session["UserReputation"] = rdr["Reputation"].ToString();

The above passes the reputation of the logged in user to the session.Now I want to pass the session to my masterpage to a Label which is placed in a LoginView. Ofcourse,I have placed my Reputation label in the of the LoginView as below :

<asp:LoginView ID="LoginView1" runat="server">
      <AnonymousTemplate>
      <asp:LoginStatus ID="LoginStatus1" runat="server" style="color: #333333"  />&nbsp;|</span>&nbsp;
      <asp:HyperLink ID="hlRegister" runat="server" style="color: #333333; text-decoration: underline">Register</asp:HyperLink>
      </AnonymousTemplate>
      <LoggedInTemplate>
      <asp:LoginName ID="LoginName1" runat="server" />&nbsp;<asp:Label ID="lblRep" runat="server" Style="background-color:#9DD929;color:white;"></asp:Label>&nbsp;
      <asp:LoginStatus ID="LoginStatus1" runat="server" style="color: #333333" onloggedout="LoginStatus1_LoggedOut" />
      </LoggedInTemplate>
</asp:LoginView>

and the i pass the session value as below :

protected void Page_Load(object sender, EventArgs e)
{
   Label rep = (Label)LoginView1.FindControl("lblRep") as Label;
   if (Session["UserReputation"].ToString() !=null)
   {
           rep.Text = Session["UserReputation"].ToString();
   }
}

My problem is that the above code keeps throwing a nullreference exception.I am just checking if

Session["UserReputation"].ToString() !=null

but it throws the exception on that very line.

Is there a way to handle this ?

Konamiman
  • 49,681
  • 17
  • 108
  • 138
RelatedRhymes
  • 428
  • 6
  • 26
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Patrick Hofman Oct 23 '15 at 08:46
  • You can't cast null object to string type. If you need to check null exception try not casting is to string `Session["UserReputation"] !=null`. – Suprabhat Biswal Oct 23 '15 at 08:47
  • @PatrickHofman - well,looks just like a duplicate,isn't it? but I have explained that the session carries a value and still throws the above exception. – RelatedRhymes Oct 23 '15 at 16:19

3 Answers3

0

You may get Session["UserReputation"] as null but you are trying to convert it to String and thus the error, do this instead:-

 if (Session["UserReputation"] !=null && rep != null)
 {
      rep.Text = Session["UserReputation"].ToString();
 }
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • have tried this before and so added ToString later but it all goes to the same error message. – RelatedRhymes Oct 23 '15 at 16:17
  • @RelatedRhymes - That's impossible! `Session["UserReputation"]` can never throw null reference exception. But `Session["UserReputation"].ToString()` will definitely throw NRE if `Session["UserReputation"]` is null. Now, what I suspect is `rep` is null so calling `rep.Text` will again throw null reference exception so you should check both. – Rahul Singh Oct 23 '15 at 18:06
0

Converting a null object to string will throw the exception so instead of converting into string and then checking if its null. Simply check if the session contains a null entry or not as stated by rahul above

if (Session["UserReputation"] !=null)
{
//your code
}
CThakur
  • 136
  • 8
0

First create a reference to LoginView then use that to reference "sub-controls." In the example below, the code references LoginStatus. It seems to work better with ClientIDMode set to Static. From LoginStatus you can create a Handler to run another function.

If you try to reference LoginStatus directly, it will be null.

Site.Master

<asp:LoginView runat="server" ViewStateMode="Disabled" ClientIDMode="Static" ID="lvStatus">
    <LoggedInTemplate>
      <ul class="nav navbar-nav">
        <li><a runat="server" href="Docs.aspx" target="_blank"><span class="glyphicon glyphicon-file">&nbsp;</span>Documents </a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a runat="server" href="#"><%: Context.User.Identity.GetUserName()  %></a></li>
        <li>
            <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutPageUrl="~/" ClientIDMode="Static" ID="logoutStatus" />
        </li>
      </ul>
    </LoggedInTemplate>
</asp:LoginView>

Page_Load of ASP.NET page:

....

    LoginView m_lvStatus = (LoginView)Master.FindControl("lvStatus");

    LoginStatus m_logoutStatus =  (LoginStatus)m_lvStatus.FindControl("logoutStatus");

    m_logoutStatus.LoggingOut += new LoginCancelEventHandler(OnLoggingOut);

}

public void OnLoggingOut(object sender, LoginCancelEventArgs e)
{
    SaveData();
}
smoore4
  • 4,520
  • 3
  • 36
  • 55