?
Solved

User.Identity.Name  as an update parameter

Posted on 2011-02-24
5
Medium Priority
?
905 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

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