Pass LoginName in InsertParameters

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!
qryzAsked:
Who is Participating?
 
TorrwinCommented:
You could try using Context.User.Identity.Name, I haven't tried that with membership and roles myself though.
0
 
deanvanrooyenCommented:
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
            ...
0
 
deanvanrooyenCommented:
personaly i think that there is more effort in my solution and more flexibility if you need to track remote ip's
0
 
GavinMannionCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.