How To Get Other ASP.NET User Profile

Posted at : Aug/04/2010
3242 Views | 0 Comments

Seperti yang telah diketahui sebelumnya bahwa mulai di ASP.NET 2.0 kita dapat menyimpan user data di object Profile. Profile dapat digunakan untuk menyimpan user preference setting atau data lainnya yang berhubungan dengan user tersebut seperti misalnya nama perusahaan dan lain-lain. Contoh yang akan dibuat kali ini yaitu bagaimana caranya mendapatkan data profile dari user tertentu yang telah disimpan dan melakukan update data profile tersebut. Apabila kita melakukan update data Profile seperti cara biasa dengan langsung memberikan nilai pada masing-masing properti profile tersebut maka data yang akan disimpan yaitu data profile untuk Current User, user yang sedang login ke web site tersebut.

Berikut langkah-langkahnya :

1). Buat Profile properties di web.config :

<system.web>

     <profile>
        <properties>
            <add name="CompanyName" type="System.String" allowAnonymous="false" />
            <add name="City" type="System.String" allowAnonymous="false" />
            <add name="Country" type="System.String" allowAnonymous="false" />
        </properties>
    </profile> 

2). Tambahkan Wizard control kedalam webform. Buat dua buah step wizard dengan step type yang pertama sebagai Start dan yang terakhir sebagai Finish. Design inputan pada masing-masing wizard step seperti pada gambar dibawah ini :

3). Tambahkan kode berikut ini yang digunakan untuk menampilkan asp.net role yang telah tersimpan,  menambahkan asp.net user secara programmatically dan sekaligus menambahkan profile untuk user baru tersebut.

    Private Sub LoadRoles()
        Dim allRoles() = Roles.GetAllRoles

        CheckBoxList1.DataSource = allRoles
        CheckBoxList1.DataBind()
    End Sub

    Protected Sub Page_Load() Handles Me.Load
        If Not IsPostBack Then LoadRoles()
    End Sub

    Protected Sub btnAddUser_Click() Handles btnAddUser.Click
        Dim strUserName = TextBox1.Text
        Dim strPassword = TextBox2.Text
        Dim strEmail = TextBox4.Text

        Dim getRoles = New List(Of String)

        For Each item As ListItem In CheckBoxList1.Items
            If item.Selected Then getRoles.Add(item.Text)
        Next

        'create new user and add the user to role(s) :
        Membership.CreateUser(strUserName, strPassword, strEmail)
        Roles.AddUserToRoles(strUserName, getRoles.ToArray)

        'get profile for new user which has been added before :
        Dim aProfile = Profile.GetProfile(strUserName)
        With aProfile
            .CompanyName = txtCompName.Text
            .City = txtCity.Text
            .Country = txtCountry.Text

            .Save()
        End With

        Wizard1.ActiveStepIndex = 1
    End Sub

Dari kode diatas dapat dilihat bahwa untuk mendapatkan nilai Profile dari user tertentu cukup dengan memanggil fungsi GetProfile dari object Profile yang mengembalikan ProfileCommon class dan setelah itu di Save. Berikut hasilnya :


 

Selamat mencoba :)


[Comments]


[Write your comment]

Name (required)

Email (required-will not published)

 
Comment
7518
Input code above below (Case Sensitive) :

ABOUT ME

Rully Yulian MF
Rully Yulian Muhammad Firmansyah | Founder & IT Trainer Native Enterprise | MCT (2008-2019) | MVP (2009-2016) | Xamarin Certified Professional | MTA | MCAD | MCPD | MOS | Bandung, West Java, Indonesia.

[Read More...]

TOP DOWNLOAD

Mapping Hak Akses User Pada MenuStrip Sampai Control Button
downloaded 6982 times

Bagaimana caranya menginstal database ketika deploying sebuah aplikasi?
downloaded 4893 times

Simple Voice Engine Application With Sound Player Class...
downloaded 4045 times

Change Group,Sort Order, Filtering By Date in Crystal Reports
downloaded 3460 times

WinForms DataGrid Paging With SqlDataAdapter
downloaded 2881 times


LINKS

CERTIFICATIONS

Xamarin Certified
MOS 2007
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench

NATIVE ENTERPRISE

Native Enterprise - IT Training

FOLLOW ME

Youtube  Facebook  Instagram  LinkedIn   Twitter

RSS


NATIVE ENTERPRISE NEWS

© Copyright 2006 - 2024   Rully Yulian MF   All rights reserved.