Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to retrieve the username from session object in c# asp.net for that i have used if else condition in aspx page of home page. But getting this error string login name does not exist in the current context how to solve this

What I have tried:

<% if (Session["username"] == null)
{ %>
    <div class="col-md-3 col-4 sign-btn">
        <a href="#" data-toggle="modal" data-target="#signModalCenter">class="fa fa-lock" href="#" data-toggle="modal" data-target="#registerModalCenter"__^ Register</a>
    </div>
    <div class="search col-md-2 col-4">
        <div class="mobile-nav-button">
            <button type="button" id="trigger-overlay">^__i class="fa fa-search"__^</i__^</button>
        </div>
        <div class="overlay overlay-door">
            <button class="overlay-close" type="button">^__i class="fa fa-times"></button>
            <form action="#" method="post" class="d-flex">
                 <input class="form-control" type="search" placeholder="Search here..." required="required" />
                 <button type="submit" class="btn btn-primary submit">^__i class="fa fa-search"></button>
            </form>
        </div>
    </div>
<%} %>
<% else
{ %>
    <div class="col-md-3 col-4 sign-btn">
        Welcome ^__strong> <% =sLoginName %> <a href="Logout.aspx" data-toggle="modal" data-target="#userModalCenter" id="ALogout">^__i class="fa fa-sign-out"> Logout</a>
    </div>
<%} %>


Code behind:
public string sLoginName = string.Empty;

in page load:
sLoginName = Session["username"].ToString();
Posted
Updated 13-Dec-18 1:09am
v2
Comments
Richard Deeming 14-Dec-18 10:30am    
Sounds like you're re-inventing the wheel. ASP.NET already has perfectly good authentication and authorization systems built-in. And if you use them, you also get to use handy controls like LoginView[^] and LoginName[^], which handle a lot of the boilerplate code for you.

Check that the definition of sLoginName is part of the page class definition, not in a subsidiary class, or a general class:
public partial class MyPage: System.Web.UI.Page
    {
    public string sLoginName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
        {
        sLoginName = Session["username"].ToString();
        }
    }
If that doesn't fix it, copy and paste the actual error message and related info instead of paraphrasing it.
 
Share this answer
 
Comments
Member 8583441 13-Dec-18 9:23am    
This is what I have tried that is why I got this error
Another option would be to define sLoginName on the page and remove it from Page_Load
C#
      </form>
    </div>
  </div>
<% }
else
{
    string sLoginName = Session["username"].ToString();
%>
    <div class="col-md-3 col-4 sign-btn">
        Welcome  <% =sLoginName %> <a href="Logout.aspx" data-toggle="modal" data-target="#userModalCenter" id="ALogout">class="fa fa-sign-out"> Logout</a>
    </div>

It may not be ideal but it can come through in a pinch
 
Share this answer
 
v3
Comments
OriginalGriff 13-Dec-18 9:29am    
*cough* ... != null *cough*
MadMyche 13-Dec-18 9:33am    
Feldercarb. Fixed
Member 8583441 13-Dec-18 9:45am    
Thanks very much sir problem solved from this solution my +5 vote to this solution
MadMyche 13-Dec-18 9:56am    
You are welcome. As I said this is not the ideal solution, just a fix for this particular page. The answer from OriginalGriff would be preferred if you are going to be working with this across multiple pages, this is more of a bandage
Member 8583441 14-Dec-18 0:48am    
I have tried that way before itself that is why i didn't the solution after having solution from you the problem is solved thanks once again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900