Solved

Accessing Login Name

Posted on 2010-08-24
7
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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