• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • Last Modified:

User.Identity.Name as an update parameter

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
Split_Pin
Asked:
Split_Pin
  • 3
  • 2
1 Solution
 
robastaCommented:
Depends on how are you binding to the grid. Are you binding through code?
0
 
Split_PinAuthor Commented:
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
 
robastaCommented:
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
 
Split_PinAuthor Commented:
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
 
Split_PinAuthor Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now