Link to home
Start Free TrialLog in
Avatar of qryz
qryz

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of Torrwin
Torrwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deanvanrooyen
deanvanrooyen

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
            ...
personaly i think that there is more effort in my solution and more flexibility if you need to track remote ip's
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.