Solved

User.Identity.Name  as an update parameter

Posted on 2011-02-24
5
888 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
[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
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

738 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