Click here to Skip to main content
13,256,716 members (51,403 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 16 Feb 2010

Profiles in .NET with Images

, 3 Apr 2012
Rate this:
Please Sign up or sign in to vote.
The best way to personalize your web application is through profiles, and ASP.NET provides lots of out of the box features to implement them.


In this article, I will talk about profiles with images. The best way to personalize your web application is through profiles, and ASP.NET provides lot of out of the box features to implement them. We all agree that an application with images looks better than an application with just plain text, and we need to store the images of users to make our web application more appealing and user friendly.

This is what I am trying to achieve.

  1. The Home page showing the image of the user, after the user logs in.
  2. After clicking on the image, change your avatar (image). A popup shows up and the user can select one from any of the available images.
  3. Files/demo3.JPG

  4. After clicking on an image of your choice, the image changes. Here, I click on "user.png":
  5. Files/demo4.JPG

Isn't that easy? I achieve all this through Profiles. And, the best part is that I do all this with less than 20 lines of code.


In this article, I will directly go into Profiles without talking about Forms Authentication, Membership, Authorization etc. I hope you find some good books and good online resources for those because there are many available.

Using the Code

Implementing Profiles is very straightforward; you just have to add a few lines in your web.config.

Here is my Profile Provider in the Web.Config of my application:

<profile defaultProvider="TestPofileProvider" automaticSaveEnabled="false" enabled="true">
<add name="TestProfileProvider" 




<add name="FirstName" type="String"/> 
<add name ="LastName" type="String"/> 
<add name="DateOfBirth" type="DateTime"/> 
<add name="Avatar" type="String"/> 
<add name="ProjectName" type="String" /> 

connectionStringName is the name of the connection string that connects to the "aspnetdb" fatabase, which has all the tables related to roles, users and profiles etc.

Properties is the list of all the properties you want to remember for a user. I would like my application to remember the user's first name, last name, date of birth, avatar (which is the image of the user) and the project name. type denotes the data type. You must be wondering why I set the data type of the image property as String. It has to be something like System.Drawing.Bitmap, isn't it? I will clarify that in the coming section. You can add any property and any number of properties you want.

Creating and Updating Profiles

In this section, I will tell you how to create Profiles in the code-behind. Creating Profiles is very straightforward, and takes only a few lines of code.

ProfileBase p1 = ProfileBase.Create("test"); //here test is the UserName
p1.SetPropertyValue("FirstName", "test");
p1.SetPropertyValue("LastName", "user");
p1.SetPropertyValue("DateOfBirth", Convert.ToDateTime("12/08/1985"));
p1.SetPropertyValue("Avatar", "Images/user.jpg");
p1.SetPropertyValue("ProjectName", "Test-Project");

If you are using a website instead of a web application, then you will get a strongly typed object called Profile (which denotes the Profile of the user logged in). You can set the values directly, like:

Profile.FirstName = "test";
Profile.LastName = "user"; 

I am using a web application, and that's why I had to set the property like p1.SetPropertyValue("FirstName", "test");, which is a little messy, but it works.

Now I think it must be clear how the Avatar data type is a String, instead of a Bitmap. Actually, I store the path of the images in the database, instead of directly storing the image as binary. By using this technique, you can save yourself a lot of coding and a lot of time. I created a folder called "Images" in my web application directory, which will store all my images.

I have created an ASPX page called Home, which will be the landing page for my application. In this page, there will be an ImageButton which will show the image of the user who is logged in and also will allow the user to change the image (avatar), and that's why I have chosen an ImageButton.

This is the ASPX markup:

<html xmlns="" > 
<head runat="server"> 
<form id="form1" runat="server"> 
<asp:ImageButton ID="ProfileImage" runat="server" 

  BorderWidth="1px" Height="100px" 

  Width="100px" BorderStyle="Outset" /> 
<asp:Panel ID="modalPanel" runat="server" 

  Visible="true" BackColor="CadetBlue" Width="250px" 

  BorderWidth="1px" BorderStyle="Outset" ScrollBars="Auto"> 
<td style="background-color:Gray;">Change Avatar </td> 
<tr> <td> 

<asp:DataList ID="AvatarView" runat="server" 

  RepeatDirection="Vertical" RepeatLayout="Flow" 

  RepeatColumns="3" CellSpacing="2" 

  ItemStyle-BorderWidth="1px" ItemStyle-BorderStyle="Groove"> 
<asp:ImageButton ID="ImageButton1" runat="server" 

  ImageUrl='<%# Eval("Name","~\\Images\\{0}") %>' 

  Height="50px" Width="50px" 

  onclick="ImageButton1_Click1" ToolTip='<%# Eval("Name") %>' /> 
<cc1:ModalPopupExtender ID="ModalPopupExtender1" 

  runat="server" TargetControlID="ProfileImage" 


As you can see from the ASPX page, I have an ImageButton named ProfileImage which will show the image and allow the user to change the image. I also have a ModalPopupExtender AJAX control which will open up a popup with all the images that are available in my Images folder (which I was referring to earlier). I have used a DataList to show all the images in that folder, and each item in the DataList is an ImageButton which causes a postback so that the path of the new avatar is updated in the Profile database.

The DataList shows all the images that are there in the Images folder. Here is how I populate my DataList. I get all the files from the directory "Images" using the DirectoryInfo class of System.IO.

DirectoryInfo dir = new DirectoryInfo(Server.MapPath("Images"));
AvatarView.DataSource = dir.GetFiles();

Here is the ASPX mark up of my DataList named AvatarView:

<asp:DataList ID="AvatarView" runat="server" 

  RepeatDirection="Vertical" RepeatLayout="Flow" 

  RepeatColumns="3" CellSpacing="2" 

  ItemStyle-BorderWidth="1px" ItemStyle-BorderStyle="Groove">
<asp:ImageButton ID="ImageButton1" runat="server" 

  ImageUrl='<%# Eval("Name","~\\Images\\{0}") %>' 

  Height="50px" Width="50px" 

  onclick="ImageButton1_Click1" ToolTip='<%# Eval("Name") %>' />

In the ItemTemplate, I use an ImageButton to show each image. I map the ImageUrl of the ImageButton to the Name property of the FileInfo object which dir.GetFiles() returns.

Here is the code to update your Profile:

protected void ImageButton1_Click1(object sender, ImageClickEventArgs e
   ImageButton newAvatar = (ImageButton)sender;
   HttpContext.Current.Profile.SetPropertyValue("Avatar", newAvatar.ImageUrl);
   ProfileImage.ImageUrl = HttpContext.Current.Profile["Avatar"].ToString();

Each button is wired to a Click event; if you click on any ImageButton in the DataList, the above event fires and it changes the Avatar property of your Profile and reloads the page so that you can see the changes.

Now, if you were to implement all this without using Forms Authentication, it would have taken a hell of a lot of time, but thanks to Forms Authentication, I was able to do this in two hours. And also, this is just the tip of the iceberg; there is so much you can do, and it all depends on your imagination, and I would say: "sky is the limit".


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anooj Nair
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

Questionregarding imagebutton Pin
Member 101971894-Sep-13 1:10
memberMember 101971894-Sep-13 1:10 
AnswerRe: regarding imagebutton Pin
Member 102502134-Sep-13 9:15
memberMember 102502134-Sep-13 9:15 
GeneralRe: regarding imagebutton Pin
Member 101971894-Sep-13 19:55
memberMember 101971894-Sep-13 19:55 
Question Pin
jafarmahmoudi28-Aug-13 14:33
memberjafarmahmoudi28-Aug-13 14:33 
Questionwhats the difference b/w session and profile than ....this can we easily do with session.... Pin
khiladi78631-Jan-13 21:59
memberkhiladi78631-Jan-13 21:59 
GeneralMy vote of 2 Pin
Naman Kumar Sinha11-Jan-13 6:43
memberNaman Kumar Sinha11-Jan-13 6:43 
Questioncan you provide the downloadable source code for this.. Pin
Khatra6-Jun-12 0:04
memberKhatra6-Jun-12 0:04 
Questiondatalist Pin
lightwalker1915-Apr-12 10:08
memberlightwalker1915-Apr-12 10:08 
QuestionImage type in Profile Pin
Mark Henke11-Apr-12 8:05
memberMark Henke11-Apr-12 8:05 
GeneralMy vote of 5 Pin
KameswaraRao2-Feb-11 20:25
memberKameswaraRao2-Feb-11 20:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 4 Apr 2012
Article Copyright 2010 by Anooj Nair
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid