Solved

User.Identity.Name  as an update parameter

Posted on 2011-02-24
5
867 Views
Last Modified: 2012-05-11
Dear Experts,

I am hoping to add the windows username (User.Identity.Name) to the update string for a gridview - as a way of keeping track of which user has last edited the data. Additionally I would like to add a timestamp. The fields exist in my database, I am just not clear on how or where in Visual Web Developer I say something like "User.Identity.Name = @username"

(ie can I refer to code variables within my ASP page ... ? I have no idea).


I am sure there is an efficient way to do this and would appreciate your thoughts.

My code behind pages are in VB


Thank you in Advance
From a newbie.


0
Comment
Question by:Split_Pin
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:robasta
ID: 34969219
Depends on how are you binding to the grid. Are you binding through code?
0
 

Author Comment

by:Split_Pin
ID: 34969284
Hi Robasta,
No (at least I don't think I am...)
I have just created the datagrid via the design window and gone through the motions to make it editable (ticking box generate Insert/Update/Delete statements).

Everything (SQL, Gridview structure, Parameters) appears to be in the ASP page.  

Thank you.
0
 
LVL 14

Accepted Solution

by:
robasta earned 125 total points
ID: 34969370
Do this:

Code Behind:
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {
            e.Command.CommandText = e.Command.CommandText.Replace("@Username","'"+ User.Identity.Name+"'");
        }

Open in new window



Page:
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:elementalConnectionString %>"        
        UpdateCommand="UPDATE [Event] SET [UserName] = @Username, [Location] = @Location, [Date] = @Date, [IsDeleted] = @IsDeleted, [SlugUrl] = @SlugUrl WHERE [ID] = @ID" 
        onupdating="SqlDataSource1_Updating">

Open in new window

0
 

Author Comment

by:Split_Pin
ID: 34976304
Hi Robasta
Sorry for the delays it was getting pretty late when I was trying this last night (in Sydney) - it didn't work straight off but I suspect that is my understanding not a problem with what you've provided.
I converted the code to VB via an online tool but it did not seem to be firing. I will try to implement over the weekend.
Cheers Split_Pin
0
 

Author Comment

by:Split_Pin
ID: 34978491
Hi again,
This is the code that seems to be working for me now - slightly different, and I have removed the onupdating= from the ASP page altogether.

Thank you for getting me on the right track. I will leave the thread open for a bit in case you want to add anything or take exception to the code appearing below.

Protected Sub SqlDataSource_ControlName_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource1.Updating

        Dim Username As String
        Username = User.Identity.Name

        e.Command.Parameters("@Username").Value = Username

    End Sub
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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