Solved

Accessing Login Name

Posted on 2010-08-24
7
284 Views
Last Modified: 2013-11-27
Newbie here,
I have a website that uses the built in login control in visual studios 2010.  I have a page that updates data in my database (all this works fine).  I now want to add the username of the person currently logged in to the row that they are updating.  How do I do this?

Here is my update command from my SQLDataSource:

UpdateCommand="UPDATE dbo.Sales_Orders SET UPC=@UPC,Product_Description=@Product_Description,Product_Quantity=@Product_Quantity,
            Product_Unit=@Product_Unit,Product_Price=@Product_Price,Comment=@Comment, LastUpdated=getdate(), UserUpdate=???? WHERE Sales_Order_ID=@Sales_Order_ID"


You can see that the last column is UserUpdate=????  How do I make it set this to the current user logged in?

Thanks for the help!
0
Comment
Question by:mattlaltman
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:thaytu888888
ID: 33517713
- Use Page.User.Identity.Name
0
 

Author Comment

by:mattlaltman
ID: 33517751
Ok I added that in replace of the ???? and I get the following error:

Incorrect syntax near the keyword 'User'.
UpdateCommand="UPDATE dbo.Sales_Orders SET UPC=@UPC,Product_Description=@Product_Description,Product_Quantity=@Product_Quantity,
            Product_Unit=@Product_Unit,Product_Price=@Product_Price,Comment=@Comment, LastUpdated=getdate(), UserUpdate=Page.User.Identity.Name WHERE Sales_Order_ID=@Sales_Order_ID"

Open in new window

0
 
LVL 6

Expert Comment

by:thaytu888888
ID: 33518421
- Try this:

UpdateCommand="UPDATE dbo.Sales_Orders SET UPC=@UPC,Product_Description=@Product_Description,Product_Quantity=@Product_Quantity,Product_Unit=@Product_Unit,Product_Price=@Product_Price,Comment=@Comment, LastUpdated=getdate(), UserUpdate=" + Page.User.Identity.Name + " WHERE Sales_Order_ID=@Sales_Order_ID"

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 6

Expert Comment

by:thaytu888888
ID: 33518468
- The codes above are used in code behind. The full statement should be:
- In designer view: UpdateCommand="UPDATE dbo.Sales_Orders SET UPC=@UPC,Product_Description=@Product_Description,Product_Quantity=@Product_Quantity,Product_Unit=@Product_Unit,Product_Price=@Product_Price,Comment=@Comment, LastUpdated=getdate(), UserUpdate=<%$Page.User.Identity.Name%> WHERE Sales_Order_ID=@Sales_Order_ID"

YourSQLDateSourceID.UpdateCommand = "UPDATE dbo.Sales_Orders SET UPC=@UPC,Product_Description=@Product_Description,Product_Quantity=@Product_Quantity,Product_Unit=@Product_Unit,Product_Price=@Product_Price,Comment=@Comment, LastUpdated=getdate(), UserUpdate='" + Page.User.Identity.Name + "' WHERE Sales_Order_ID=@Sales_Order_ID";

Open in new window

0
 

Author Comment

by:mattlaltman
ID: 33527721
Ok I tried all those solutions and they did not work.  Maybe I should be more clear.  This is no in a code behind page.  I included more of the code, the GridView and the SqlDataSource.  Will that help explain the situation?

As you can see I tried to accomplish this with the ProfileParameter but this didn't work either.  I'm not even sure if that is what I need. I've been trying multiple things.  

Thanks for your continued help
<asp:GridView
        id="grdSales"
        DataKeyNames="Sales_Order_ID"
        DataSourceID="srcSales"
        Runat="server" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" /> 



<asp:SqlDataSource
        id="srcSales"
        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
        SelectCommand="SELECT Sales_Order_ID, UPC, Product_Description, Product_Quantity, Product_Unit,  Product_Price, Comment
            FROM dbo.Sales_Orders WHERE Invoice_Number=@Invoice_Number"
        UpdateCommand="UPDATE dbo.Sales_Orders SET UPC=@UPC,Product_Description=@Product_Description,Product_Quantity=@Product_Quantity,
            Product_Unit=@Product_Unit,Product_Price=@Product_Price,Comment=@Comment, LastUpdated=getdate(), UserUpdate=??? WHERE Sales_Order_ID=@Sales_Order_ID"
        Runat="server">
        <SelectParameters>
        <asp:ControlParameter 
            Name="Invoice_Number"
            ControlID="grdSalesCategories"
            PropertyName="SelectedValue" />
        </SelectParameters>
        <UpdateParameters>
            <asp:ProfileParameter Name="Name" PropertyName="DisplayName" />
        </UpdateParameters>

    </asp:SqlDataSource>

Open in new window

0
 

Author Comment

by:mattlaltman
ID: 33533546
I'm starting to think that the ProfileParameter is what I need.  I just can't get it to work.  Then I would set UserUpdate=Name.  Maybe I have something wrong in the webconfig.

Still searching.

http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx
0
 

Accepted Solution

by:
mattlaltman earned 0 total points
ID: 33545833
Ok I figured it out.

You set UserUpdate=@Name in the datasource and then create a profile parameter called "Name"

Then you set up a property in the config like this:

            <properties>
                <add name="DisplayName" defaultValue="Anonymous"/>
            </properties>

Then in the code behind page you add a page load event that sets the current user name to that profile property

Profile.DisplayName = User.Identity.Name



If there is an easier way then please share.  Thanks
<asp:SqlDataSource
        id="srcSales"
        ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
        SelectCommand="SELECT Sales_Order_ID, UPC, Product_Description, Product_Quantity, Product_Unit,  Product_Price, Comment
            FROM dbo.Sales_Orders WHERE Invoice_Number=@Invoice_Number"
        UpdateCommand="UPDATE dbo.Sales_Orders SET UPC=@UPC,Product_Description=@Product_Description,Product_Quantity=@Product_Quantity,
            Product_Unit=@Product_Unit,Product_Price=@Product_Price,Comment=@Comment, LastUpdated=getdate(), UserUpdate=@Name WHERE Sales_Order_ID=@Sales_Order_ID"
        Runat="server">
        <SelectParameters>
        <asp:ControlParameter 
            Name="Invoice_Number"
            ControlID="grdSalesCategories"
            PropertyName="SelectedValue" />
        </SelectParameters>
        <UpdateParameters>
            <asp:ProfileParameter Name="Name" PropertyName="DisplayName" />
        </UpdateParameters>

    </asp:SqlDataSource>



Code Behind Page in VB:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Profile.DisplayName = User.Identity.Name

    End Sub


Web Config:
        <profile>
            <properties>
                <add name="DisplayName" defaultValue="Anonymous"/>
            </properties>
        </profile>

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 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