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 March 3rd, 2006, 03:06 PM
Authorized User
 
Join Date: Feb 2006
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default storing the login name in my personal database

Hi folks,

does anyone know how to pick the login name that the user type to access the website and store it in a personal database.

what i did:

i took the 'login name' from the toolbox and add it to my page. once the person logs in, his name is displayed on the 'login name'. in my case i want to take that log in name and store it in one of my database tables. so what i did was as follow:

i took the 'username id' from the properties which is [u]'loginname1'</u> and assigned it to this code:
objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = LoginName1.ToString
this then will be stored in my table under the 'requestedby' column. however, data has been successfully entered but when i checked the table, i found under the 'requedby' column the following name: System.Web.UI.WebControls.LoginName.
i have big fate that someone knows how to do this. can you please?

 
Old March 3rd, 2006, 04:01 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You are accessing the object itself, not the user name textbox. Try:
objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = LoginName1.Username

 
Old March 3rd, 2006, 05:30 PM
Authorized User
 
Join Date: Feb 2006
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you very much jbenson001. what i would like to say here is that the loginname1 is dragged from the toolbox under the login tab. if you check it you will only see that it has a id property and not a text property.

so the problem here is how can i get the name of the loginname1 assigned to my column after i submit the data.

thanks for all the helps.

 
Old March 3rd, 2006, 08:07 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

The LoginName control is just to do what its name implies: display the login name and nothing more. So, it doesn't have properties like UserName.

Instead, use this:

string userName = User.Identity.Name;

or

string userName = Membership.GetUser().UserName;

to get the user's name.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old March 4th, 2006, 12:20 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

A login control has a username textbox and password textbox, a loginview DOES NOT, as Imar has stated. So, if you are using the login control, you can access it. You are probably best using Imar's code. This way when you get the name, you know the user has been authenticated.

 
Old March 4th, 2006, 02:12 PM
Authorized User
 
Join Date: Feb 2006
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you very much for everyone.

the following script has worked:
objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = user.identity.name


 
Old June 8th, 2006, 03:50 PM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am having the same issue, however, I am new to codeing. Either I am an idiot or this is so simple it is staring me in the face.

Originally I wanted to extend the aspnet_User table. Not an option. I need to store much more information about each user. So... I created an Accounts table with the fields I needed ( pkAccountID, FirstName, lastName, homePhone, etc). I added a field to store their username (accountUsername, nvarchar(256)) just like the aspnet_User.UserName field.

Okay...now, after their account is created they are redirected to an account information page that is attached to this database with a Details View, (default view INSERT). I need to pass the loginName from the aspnet_User table into the accountUserName field so that I can use that as a filter for them to add and update their information later.

I need to know in very basic terms what code I need, exactly where to put it, etc. This post is helpful, but WHERE do I put the code

objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = LoginName1.Username

and/OR

string userName = Membership.GetUser().UserName;


Here is my code:
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;Welcome
        <asp:LoginName ID="lblLogInName" runat="server" />
        !<br />
        <br />
    </div>
        &nbsp; &nbsp; &nbsp;
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
            DefaultMode="Insert" Height="50px" Width="125px" DataKeyNames="loginName" AutoGenerateInsertButton="True" DataMember="DefaultView" EmptyDataText="No Data">
            <Fields>
                <asp:TemplateField HeaderText="accountUserName" SortExpression="accountUserName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:Label ID="lblAcctUserName" runat="server" OnDataBinding="Page_Load" Text='<%# Eval("accountUserName") %>'></asp:Label>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
                <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionName %>"
            DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO tblUserDetails(accountUserName, accountFirstName, accountLastName) VALUES (@UserName, @accountFirstName, @accountLastName)"
            ProviderName="<%$ ConnectionStrings:connectionName.ProviderName %>"
            SelectCommand="SELECT accountUserName, accountFirstName, accountLastName FROM tblUserDetails"
            UpdateCommand="UPDATE [tblUserDetails] SET [accountUserName] = 'TEST', [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
            <DeleteParameters>
                <asp:Parameter Name="accountID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="UserName" />
                <asp:Parameter Name="accountFirstName" Type="String" />
                <asp:Parameter Name="accountLastName" Type="String" />
                <asp:Parameter Name="accountID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="UserName" DefaultValue = "Test"
            />
                <asp:Parameter Name="accountFirstName" Type="String" />
                <asp:Parameter Name="accountLastName" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        &nbsp;
    </form>
</body>

Once they leave this screen they will not go back, will never change their username, etc. I only need to create an entry in this table ONE time so that I can use it for future.

Anyone who can help me would be soooo appreciated. I am overdue on this project, my client is furious and I have searched high and low on specific instructions on how to do this for two days.

Thank you!!!



 
Old June 8th, 2006, 04:10 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi shario,

What you can do, is override the value of the UserName property inside the Inserting event of the DetailsView:

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventAr gs) _
   Handles DetailsView1.ItemInserting

  e.Values("UserName") = User.Identity.Name

End Sub

The Values collection holds a collection with name / value items thar are to be sent to the database. By accessing the UserName item you can override / set its value with the user's name.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
The Following User Says Thank You to Imar For This Useful Post:
sg48 (December 11th, 2008)
 
Old June 9th, 2006, 01:14 AM
Authorized User
 
Join Date: Jun 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar -

OMG!!! How much do I love you? That worked perfectly. I noticed that you answer a lot of postings and I want to thank you, thank you, thank you!

I started the next text and...Next challenge. I thought when that when I had a field in the database that matched the username, I could use a WHERE clause in my database to filter (ie. accountUserName = username)... This is not working.

Here is the scenario
  1. User creates account.
  2. Login lands at the form we just created and enters detailed information in tblAccounts. Username populates accountUserName field (thanks to you). Submit takes them to a page accountDetails.aspx, a detailsview that is filtered by username.
3. User Logs out
4. When User logs back in, the login takes them back to accountDetails.aspx filtered by the username. They can edit details if needed. (WHERE accountUserName = username).

I created the accountDetails.aspx page and filtered the datasource by session field username.

Here is the code from the form...

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataKeyNames="" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
        <Fields>
            <asp:BoundField DataField="accountID" HeaderText="accountID" InsertVisible="False"
                ReadOnly="True" SortExpression="accountID" />
            <asp:BoundField DataField="accountUserName" HeaderText="accountUserName" SortExpression="accountUserName" />
            <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
            <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connCPFAAEvents %>"
        DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO [tblUserDetails] ([accountUserName], [accountFirstName], [accountLastName]) VALUES (@accountUserName, @accountFirstName, @accountLastName)"
        ProviderName="<%$ ConnectionStrings:connectionName.ProviderName %>"
        SelectCommand="SELECT [accountID], [accountUserName], [accountFirstName], [accountLastName] FROM [tblUserDetails] WHERE ([accountUserName] = @accountUserName)"
        UpdateCommand="UPDATE [tblUserDetails] SET [accountUserName] = @accountUserName, [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
        <DeleteParameters>
            <asp:Parameter Name="accountID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="accountID" Type="Int32" />
        </UpdateParameters>
        <SelectParameters>
            <asp:SessionParameter Name="accountUserName" SessionField="username" Type="String" />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>


Thank you again!






P.S. Just to clarify for visitors to this post, I am using code-behind. All I had to do was open the webpage.aspx.vb file and paste the text into the page.

 
Old June 9th, 2006, 01:31 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi shario,

You're welcome. Glad it's working now.

Not sure if you are asking a new question, or explaining to others how you made it work. If it's a question, you seem to be using a Session field in the WHERE clause for the SELECT command. Are you sure that this session variable contains the user's name?

Alternatively, you can perform the same trick as I showed you earlier with the Selecting event:
Code:
  Protected Sub ObjectDataSource1_Selecting(ByVal sender As Object, _
          ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) _
          Handles ObjectDataSource1.Selecting
    e.InputParameters("UserName") = User.Identity.Name
  End Sub
  Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing images in Database and displaying them Manoj Bisht ASP.NET 1.x and 2.0 Application Design 4 February 6th, 2010 03:06 AM
Storing Images into database usprasad_m SQL Server 2000 4 May 17th, 2007 05:16 PM
Storing Videos to Database... ankur_icfai SQL Server 2000 2 October 19th, 2006 01:18 AM
Chapter 13 - Storing Inf in a Database phudong3da Dreamweaver (all versions) 1 April 22nd, 2005 01:00 AM
storing an image in a database nagdandey Classic ASP Databases 2 December 17th, 2003 05:39 AM





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