Solved

User.Identity.Name  as an update parameter

Posted on 2011-02-24
5
857 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

16 Experts available now in Live!

Get 1:1 Help Now