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" /> |</span>
<asp:HyperLink ID="hlRegister" runat="server" style="color: #333333; text-decoration: underline">Register</asp:HyperLink>
</AnonymousTemplate>
<LoggedInTemplate>
<asp:LoginName ID="LoginName1" runat="server" /> <asp:Label ID="lblRep" runat="server" Style="background-color:#9DD929;color:white;"></asp:Label>
<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 ?