<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Eduzine© - C#</title>
    <link>http://eduzine.edujini-labs.com/</link>
    <description>Articles from Edujini Team</description>
    <dc:language>en</dc:language>
    <admin:errorReportsTo rdf:resource="mailto:" />
    <generator>Serendipity 0.8.2 - http://www.s9y.org/</generator>
    <pubDate>Mon, 12 Feb 2007 20:16:41 GMT</pubDate>

    <image>
        <url>http://www.edujini-labs.com/images/newmasthead.gif</url>
        <title>RSS: Eduzine© - C# - Articles from Edujini Team</title>
        <link>http://eduzine.edujini-labs.com/</link>
        <width></width>
        <height></height>
    </image>
<item>
    <title>HOWTO: How to work with custom profile-class in ASP.Net</title>
    <link>http://eduzine.edujini-labs.com/archives/23-HOWTO-How-to-work-with-custom-profile-class-in-ASP.Net.html</link>
<category>ASP.Net</category><category>C#</category>    <comments>http://eduzine.edujini-labs.com/archives/23-HOWTO-How-to-work-with-custom-profile-class-in-ASP.Net.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=23</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=23</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;
It is easy to customize profile by adding entries in &lt;code&gt;web.config&lt;/code&gt;. But the custom-entries added are available only in the codebeside classes and not in any other class.
&lt;/p&gt;

&lt;p&gt;
The situation worsens if the profile entries need to be made available in custom web-control that may reside in another assembly. As such, the entries must be available at the compile time of the control-assembly and not of the website.
&lt;/p&gt;

&lt;p&gt;
This article descibes a way out to the problem...
&lt;/p&gt;


 
 

&lt;h2&gt;Solution&lt;/h2&gt;

&lt;p&gt;
Let us say that we need three entries in profile - &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;city&lt;/code&gt; - of which &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;city&lt;/code&gt; are &lt;code&gt;string&lt;/code&gt; while age is &lt;code&gt;int&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
First step is to create a class, say, &lt;code&gt;EdujiniProfile&lt;/code&gt; as given below:
&lt;/p&gt;

&lt;pre&gt;using System.Web.Profile;&lt;br /&gt;&lt;br /&gt;namespace Edujini.Tutorials.ASPNet&lt;br /&gt;{&lt;br /&gt;    public class EdujiniProfile : ProfileBase&lt;br /&gt;    {&lt;br /&gt;        #region Fields&lt;br /&gt;        private string name;&lt;br /&gt;        private int age;&lt;br /&gt;        private string city;&lt;br /&gt;        #endregion&lt;br /&gt;&lt;br /&gt;        public EdujiniProfile()&lt;br /&gt;        {&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        #region Properties&lt;br /&gt;        public string City&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return city;&lt;br /&gt;            }&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                city = value;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public int Age&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return age;&lt;br /&gt;            }&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                age = value;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public string Name&lt;br /&gt;        {&lt;br /&gt;            get&lt;br /&gt;            {&lt;br /&gt;                return name;&lt;br /&gt;            }&lt;br /&gt;            set&lt;br /&gt;            {&lt;br /&gt;                name = value;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        #endregion&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;
The next step is to customize &lt;code&gt;web.config&lt;/code&gt; to ensure that the &amp;lt;profile&amp;gt; entry look similar to that as given below:
&lt;/p&gt;


&lt;pre&gt;&amp;lt;profile enabled=&amp;quot;true&amp;quot;&lt;br /&gt;        defaultProvider=&amp;quot;MyProfileProvider&amp;quot; &lt;br /&gt;        inherits=&amp;quot;Edujini.Tutorials.ASPNet.EdujiniProfile&amp;quot;&amp;gt;&lt;br /&gt;    &amp;lt;providers&amp;gt;&lt;br /&gt;        &amp;lt;add name=&amp;quot;MyProfileProvider&amp;quot;&lt;br /&gt;            type=&amp;quot;System.Web.Profile.SqlProfileProvider&amp;quot;&lt;br /&gt;            connectionStringName=&amp;quot;ASPNetAuthDB&amp;quot;/&amp;gt;&lt;br /&gt;    &amp;lt;/providers&amp;gt;&lt;br /&gt;    &amp;lt;properties&amp;gt;&lt;br /&gt;        &amp;lt;add name=&amp;quot;Country&amp;quot; type=&amp;quot;System.String&amp;quot;/&amp;gt;&lt;br /&gt;    &amp;lt;/properties&amp;gt;&lt;br /&gt;&amp;lt;/profile&amp;gt;&lt;/pre&gt;


&lt;p&gt;
I have added yet another field called &lt;code&gt;Country&lt;/code&gt; of type &lt;code&gt;string&lt;/code&gt;.
&lt;/p&gt;

&lt;p&gt;
Let us say that we need to refer to the profile in a class &lt;code&gt;ProfileManipulator&lt;/code&gt;. Here's how it can get reference to the profile for a specific user:
&lt;/p&gt;

&lt;pre&gt;namespace Edujini.Tutorials.ASPNet&lt;br /&gt;{&lt;br /&gt;    public static class ProfileManipulator&lt;br /&gt;    {&lt;br /&gt;        public static EdujiniProfile GetProfileDetails(string username)&lt;br /&gt;        {&lt;br /&gt;            HttpContext context = HttpContext.Current;&lt;br /&gt;            if(context.Request.IsAuthenticated)&lt;br /&gt;            {&lt;br /&gt;                ProfileBase baseProfile = ProfileBase.Create(username);&lt;br /&gt;                EdujiniProfile profile = baseProfile as EdujiniProfile;&lt;br /&gt;&lt;br /&gt;                return profile;&lt;br /&gt;            }&lt;br /&gt;            return null;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;}&lt;/pre&gt;

&lt;p&gt;
All we need to ensure that the user is logged in. Note that the most popular method used in the codebeside in the &lt;code&gt;Page&lt;/code&gt; derived class to get access to the profile is
&lt;/p&gt;
&lt;pre&gt;ProfileCommon profile = base.Profile.GetProfile(username);&lt;/pre&gt;

&lt;p&gt;
In the new system, i.e., the backend class - &lt;code&gt;ProfileManipulator&lt;/code&gt;, we cannot type-cast to &lt;code&gt;ProfileCommon&lt;/code&gt; since it does not exist yet!
&lt;/p&gt;

&lt;p&gt;
Proceed ahead and use the new profile!
&lt;/p&gt;

    </content:encoded>
    <pubDate>Sun, 28 Jan 2007 01:08:33 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/23-guid.html</guid>
    </item>
</channel>
</rss>
