Pass LoginName in InsertParameters

Posted on 2006-04-05
Last Modified: 2012-06-21
Whenever a record is inserted into the database, I would like to store the username of the user that inserted the record by passing their login name as a parameter in the SqlDataSource.  How would I go about doing that?  I am using ASP .Net Membership and Roles.

Thanks for your help!
Question by:qryz
    LVL 13

    Accepted Solution

    You could try using Context.User.Identity.Name, I haven't tried that with membership and roles myself though.
    LVL 12

    Expert Comment

    you can get the authenticated user name from the server variables -  eg once they are authenticated then the user name is stored in the a spefic key AUTH_USER in a server vairable. Below shows how to get some info from these keys in c#...

    make sure these are added
    using System.Collections;
    using System.Collections.Specialized;

                //code below
                DateTime dt = new DateTime();
                dt = DateTime.Now;
                string date = dt.ToString("s", System.Globalization.DateTimeFormatInfo.InvariantInfo);
                date = date.Replace('T', ' '); //datetime now yyyymmdd HHmmss

                //get collection of server variables
                NameValueCollection coll;
                coll = obj.ServerVariables;

                String[] arr1 = coll.AllKeys;

                // set sql string
                string insert = "insert into users_log(username,pswd,AUTH_TYPE,AUTH_USER,REMOTE_USER,REMOTE_ADDR,REMOTE_HOST,SCRIPT_NAME,URL,HTTP_USER_AGENT,datetime) " +
                                "values('" + username + "','" + pswd + "','" +
                                coll.GetValues("AUTH_TYPE").GetValue(0) + "','" +
                                coll.GetValues("AUTH_USER").GetValue(0) + "','" +
                                coll.GetValues("REMOTE_USER").GetValue(0) + "','" +
                                coll.GetValues("REMOTE_ADDR").GetValue(0) + "','" +
                                coll.GetValues("REMOTE_HOST").GetValue(0) + "','" +
                                coll.GetValues("SCRIPT_NAME").GetValue(0) + "','" +
                                coll.GetValues("URL").GetValue(0) + "','" +
                                coll.GetValues("HTTP_USER_AGENT").GetValue(0) + "','" +
                                date + "')";
                //check sql
                //insert sql into db
    LVL 12

    Expert Comment

    personaly i think that there is more effort in my solution and more flexibility if you need to track remote ip's
    LVL 15

    Expert Comment

    That may be the case, but the original question was

    "passing their login name as a parameter in the SqlDataSource"

    Since only the login name was requested, Torrwin's simple posting would have given the correct requirement.
    Our rule of thumb is first in gets the points.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now