Hi AliTeoman,
AliTeoman says:
Context.User.Identity.GetUserName()
You need to use User.Identity.Name property to get the logged in user name.
For filtering the record you need to pass the parameter to the SqlDataSource but you have to use SelectParameters.
First add a HiddenField to the page.
Then set the HiddenField with loggedin user name using User.Identity.Name property inside the Page Load event is user is valid.
After that set ControlParameter ControlID with HiddenField ID and Name with parameter name set the select query.
Using the below article i have created the example.
Here i am sharing the Home page code.
The login page code remain same as in the article.
HTML
<div>
Welcome
<asp:LoginName ID="LoginName1" runat="server" Font-Bold="true" />
<br />
<br />
<asp:Label ID="lblLastLoginDate" runat="server" />
<asp:LoginStatus ID="LoginStatus1" runat="server" />
<hr />
<asp:HiddenField ID="hfUserName" runat="server" />
<asp:GridView ID="gvUsers" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:constr %>'
SelectCommand="SELECT * FROM Users WHERE UserName = @UserName">
<SelectParameters>
<asp:ControlParameter ControlID="hfUserName" Type="String" Name="UserName" />
</SelectParameters>
</asp:SqlDataSource>
</div>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.User.Identity.IsAuthenticated)
{
System.Web.Security.FormsAuthentication.RedirectToLoginPage();
}
else
{
hfUserName.Value = this.Page.User.Identity.Name;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.Page.User.Identity.IsAuthenticated Then
FormsAuthentication.RedirectToLoginPage()
Else
hfUserName.Value = Me.Page.User.Identity.Name
End If
End Sub
Screenshot