Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 10th, 2008, 01:35 PM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default insert the username of the logged in user

How do I insert the UserName of the Logged in user into the
DB field UserID. I created a web form page, and added a form
view control set the page default to Insert Mode. The Page is used to insert
data. I have a hidden field called UserID that I would like to capture the Logged in
user. I have been trying to figure out what I am doing worng, but no luck, Please suggest
the best way to do this. I have listed what I have done thus far?

System.aspx---------------------------------------------------------------------------------------------------------------
1) <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="SystemID"
        DataSourceID="SystemSqlDataSource1" DefaultMode="Insert" Width="583px">

2) <asp:TextBox ID="UserIDTextBox" runat="server"
                Text='<%# Bind("UserID") %>' Visible="False"></asp:TextBox><br />

3) <asp:SqlDataSource ID="SystemSqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RCMISConnectionString %>"
        DeleteCommand="DELETE FROM [tblSystem] WHERE [SystemID] = @SystemID" InsertCommand="INSERT INTO [tblSystem] ([SystemID], [SystemDesc], [Inactive], [TimeStampEntry], [TimeStampUpdate], [UserID]) VALUES (@SystemID, @SystemDesc, @Inactive, GETDATE(), @TimeStampUpdate, @UserID)"

4) <InsertParameters>
            <asp:Parameter Name="SystemID" Type="String" />
            <asp:Parameter Name="SystemDesc" Type="String" />
            <asp:Parameter Name="Inactive" Type="Boolean" />
            <asp:Parameter Name="TimeStampEntry" Type="DateTime" />
            <asp:Parameter Name="TimeStampUpdate" Type="DateTime" />
            <asp:Parameter Name="UserID" Type="String" />
            <asp:Parameter Name="@UserId" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

System.aspx.vb---------------------------------------------------------------------------------------------------------
5) Partial Class MemberPages_RCM_frmSystem
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'UserIDTextBox.Text = Membership.GetUser().ProviderUserKey.ToString()

    End Sub

    Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArg s) Handles FormView1.ItemInserted
        'Set the UserID Value to the currently logged on user's ID
        e.Values("UserID") = Membership.GetUser().ProviderUserKey

    End Sub

End Class


Thank you in advance,

 
Old February 11th, 2008, 04:31 AM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

I think it may be getting confused by the double UserId entries...

           <asp:Parameter Name="UserID" Type="String" />
           <asp:Parameter Name="@UserId" Type="String" />

Delete the second line, the first is the proper format for a parameter. Then try it again.

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old February 11th, 2008, 12:34 PM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I removed the line, <asp:Parameter Name="@UserId" Type="String" />, logged in to the page and added a record clicking on the form view insert button. I checked the DB table and the userID(userName) field is blank. Where should I look now.

 
Old February 11th, 2008, 01:27 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

From the page context, I'm pretty sure the logged in username is here:

   Me.User.Identity.Name


-Peter
 
Old February 11th, 2008, 08:57 PM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

do you have an example?

 
Old February 12th, 2008, 05:03 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Um... I posted the exact code you use. What more of an example are you looking for?

-Peter
 
Old February 12th, 2008, 10:13 AM
Authorized User
 
Join Date: Jun 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello and thank you for your direction, I used the following to see the value added to the DB table.

 Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
        'Set the UserID Value to the currently logged on user's ID
        'e.Values("UserID") = Membership.GetUser().ProviderUserKey
        SystemSqlDataSource1.InsertParameters("UserName"). DefaultValue = User.Identity.Name
    End Sub

Thank you,






Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting logged in UserName to Session ID Admiral1701 ASP.NET 2.0 Professional 10 December 1st, 2006 10:35 AM
user logged jonyBravo Access 6 November 27th, 2006 09:14 AM
Username of logged in user spardoe BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 4 August 18th, 2006 01:13 AM
Getting the name of the logged on user Grahame2003 C# 2 March 4th, 2004 04:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.