Solved

Accessing Login Name

Posted on 2010-08-24
7
286 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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 Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

734 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