Categories
Geeky/Programming

Visual Studio 2005/2008 Web Application Projects: Profile Class Auto Generation Workaround

I have been doing some coding with VS2008, and have been playing with the .NET Membership, Role, and Profile providers. I don’t use "Web Sites" when I create projects, instead I use "Web Applications" – what is the difference? Web Sites don’t really act like a first class application, there are some weird things with references, the bin directory, etc. Web Application act like a console app, or windows form app, the way you can reference assemblies, etc. There are a ton more differences I am sure, but those are the ones that stand out for me.

Now, with the Profile stuff in ASP.NET, you can use a Login control, and CreateUserWizard control, and then hook up your site to a SQL backend using the ASP.NET Membership provider. Everything works great. But you can also add custom properties (ex: First Name, Last Name, etc) to your user’s "Profile". You add these settings in the Web.Config

<profile enabled="true">
    <properties>
        <add name="FirstName" type="string"/>
        <add name="LastName" type="string"/>
        <group name="Address">
            <add name="Address1" type="string"/>
            <add name="Address2" type="string"/>
            <add name="City" type="string"/>
            <add name="State" type="Int32"/>
            <add name="Country" type="Int32"/>
            <add name="PostalCode" type="string"/>
        </group>
        <add name="PhoneNumber" type="string"/>
    </properties> 
</profile>

and then when you build your project, Visual Studio is supposed to auto create a class behind the scenes with those properties. This works fine if you use a "Web Site", but fails to do so when you have a "Web Application"

In VS2005, there was an add in you could install that would auto create this class on build, and there is one as well for VS2008 – Web Profile Builder.

You can use the Web Profile Builder which just creates a class for you, or you can create your own, or you can just code around it, like this:

CreateUserWizard cuwWiz = (CreateUserWizard)lgnView.FindControl("CreateUserWizard1");

ProfileBase p = ProfileBase.Create(cuwWiz.UserName, true);

p.SetPropertyValue("FirstName", ((TextBox)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("txtFirstName")).Text);
p.SetPropertyValue("LastName", ((TextBox)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("txtLastName")).Text);

p.GetProfileGroup("Address").SetPropertyValue("Address1", ((TextBox)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("txtAddress1")).Text);
p.GetProfileGroup("Address").SetPropertyValue("Address2", ((TextBox)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("txtAddress2")).Text);
p.GetProfileGroup("Address").SetPropertyValue("City", ((TextBox)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("txtCity")).Text);
p.GetProfileGroup("Address").SetPropertyValue("State", Globals.IntParse(((DropDownList)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("ddlState")).SelectedValue));
p.GetProfileGroup("Address").SetPropertyValue("PostalCode", ((TextBox)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("txtPostalCode")).Text);
p.GetProfileGroup("Address").SetPropertyValue("Country", Globals.IntParse(((DropDownList)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("ddlCountry")).SelectedValue));

p.SetPropertyValue("PhoneNumber", ((TextBox)cuwWiz.CreateUserStep.ContentTemplateContainer.FindControl("txtPhone")).Text);

p.Save();

Using ProfileBase lets you get around the use of the auto generated class, or the hand created class you might make. It does make it a little harder to set and get the properties though. If you did have the WebProfile or ProfileCommon class, you could just say

wp.FirstName = "Steve";

I wrestled with this profile stuff here for a good hour or two before I finally found on the net that Web Applications don’t auto create the class, etc. The class that it does generate though is derived from ProfileBase, so you can just use that and be OK. It would be nice if Web Application Projects did create the class though, it would have saved me a couple of head scratching hours. 🙂

By Steve Novoselac

Director of Digital Technology @TrekBikes, Father, Musician, Cyclist, Homebrewer

6 replies on “Visual Studio 2005/2008 Web Application Projects: Profile Class Auto Generation Workaround”

Hi Steve,

Thanks for your explanation.
There is however something I don’t understand
Where is stored the data after p.Saved is called.
I thought it was in the aspnetdb aspnet_Profile table.
I tried your code. It compiles fine. It runs well. And I can see my new user in the aspnet_Membership table, but not the FirstName, LastNale, … fro the profile.
Do you have an idea of what I missed?

Daniel

Like

Leave a reply to View Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.