Hi vereato,
Already user name saved in session.
You are recalling the database code to fetch user name based on the session which is not required.
If you want more details the only call the database code.
Also you need to call the code in page load event.
Check this example. Now please take its reference and correct your code.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Merienda+One" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<nav class="navbar navbar-expand-xl navbar-light bg-light">
<a href="#" class="navbar-brand"><i class="fa fa-cube"></i>ASPSnippets</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div id="navbarCollapse" class="collapse navbar-collapse justify-content-start">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">About</a>
<div class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown">Services</a>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">Web Design</a>
<a href="#" class="dropdown-item">Web Development</a>
<a href="#" class="dropdown-item">Graphic Design</a>
<a href="#" class="dropdown-item">Digital Marketing</a>
</div>
</div>
<a href="#" class="nav-item nav-link">Blog</a>
<a href="#" class="nav-item nav-link">Contact</a>
</div>
<div class="input-group search-box">
<input type="text" id="search" class="form-control" placeholder="Search by Name" />
<span class="input-group-addon"><i class="material-icons"></i></span>
</div>
<div class="navbar-nav ml-auto">
<a href="#" class="nav-item nav-link notifications"><i class="fa fa-bell-o"></i><span class="badge">1</span></a>
<a href="#" class="nav-item nav-link messages"><i class="fa fa-envelope-o"></i><span class="badge">10</span></a>
<div class="nav-item dropdown">
<div data-toggle="dropdown" class="nav-link dropdown-toggle user-action">
<img src="canada.png" class="avatar" alt="Avatar" />
<asp:LinkButton ID="Dashboard_LinkBtn_User_Name" runat="server"
FontFamily="Rockwell" Font-Bold="True" Font-Size="16px" ForeColor="Gold">
</asp:LinkButton>
<b class="caret"></b>
</div>
</div>
</div>
</div>
</nav>
</form>
</body>
</html>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Session["User_Name"] != null)
{
Dashboard_LinkBtn_User_Name.Text = Session["User_Name"].ToString();
Dashboard_LinkBtn_User_Name.Visible = true;
}
}
VB.Net
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Session("User_Name") Is Nothing Then
Dashboard_LinkBtn_User_Name.Text = Session("User_Name").ToString()
Dashboard_LinkBtn_User_Name.Visible = True
End If
End Sub
Screenshot
Note: Make sure you set the Username in Session.