Solved

Accessing Login Name

Posted on 2010-08-24
7
281 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now