Solved

Accessing Login Name

Posted on 2010-08-24
7
285 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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