If you are using ASP.NET Membership Provider for a website, one simple and practical way how to get ‘username’ for logging activities on CRUD (Create Read Update Delete) implementation is by storing the “username” on a session. Put this little code on Default page, or first landing page after succesfull user login.

using System.Web.Security;

 protected void Page_Load(object sender, EventArgs e)
    {
        //check user login
        if (User.Identity.IsAuthenticated)
        {
            //get username and put into session
            Session["UserName"] = Membership.GetUser().UserName.ToString();
        }
    }

Next example, if you are using DataSet , just put the session name on a parameter for later use on SQL Command.

<InsertParameters>
<asp:SessionParameter DefaultValue="na" Name="UserName" SessionField="UserName" Type="String" />
<InsertParameters>

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.