Smitka
Smitka

Reputation: 57

ProfileCommon could be not found

I got error ProfileCommon could be not found , in my code. I don't know how to fix the error. I put namespace using system.Web.Profile, but error still does here. Could someone help how to do that? Please help me if you know. Thank you

public partial class UserProfile : System.Web.UI.UserControl
{
    private string _userName = "";
    public string UserName
    {
        get { return _userName; }

        set { _userName = value; }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        this.Page.RegisterRequiresControlState(this);
    }

    protected override void LoadControlState(object savedState)
    {
        object[] ctlState = (object[])savedState;
        base.LoadControlState(ctlState[0]);
        _userName = (string)ctlState[1];
    }

    protected override object SaveControlState()
    {
        object[] ctlState = new object[2];
        ctlState[0] = base.SaveControlState();
        ctlState[1] = _userName;
        return ctlState;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            // if the UserName property contains an emtpy string, retrieve the profile
            // for the current user, otherwise for the specified user
            ProfileCommon profile = this.Profile;
            if (this.UserName.Length > 0)
                profile = this.Profile.GetProfile(this.UserName);
            txtFirstName.Text = profile.FirstName;
            txtLastName.Text = profile.LastName;
            ddlGenders.SelectedValue = profile.Gender;
            if (profile.BirthDate != DateTime.MinValue)
                txtBirthDate.Text = profile.BirthDate.ToShortDateString();
            ddlOccupations.SelectedValue = profile.Occupation;
            txtWebsite.Text = profile.Website;
            txtStreet.Text = profile.Address.Street;
            txtCity.Text = profile.Address.City;
            txtPostalCode.Text = profile.Address.PostalCode;
            txtState.Text = profile.Address.State;
            txtPhone.Text = profile.Contacts.Phone;
            txtFax.Text = profile.Contacts.Fax;
        }
    }
    public void Save()
    {
        // if the UserName property contains an emtpy string, save the current user's
        // profile, othwerwise save the profile for the specified user
        ProfileCommon profile = this.Profile;
        if (this.UserName.Length > 0)
            profile = this.Profile.GetProfile(this.UserName);
        profile.FirstName = txtFirstName.Text;
        profile.LastName = txtLastName.Text;
        profile.Gender = ddlGenders.SelectedValue;
        if (txtBirthDate.Text.Trim().Length > 0)
            profile.BirthDate = DateTime.Parse(txtBirthDate.Text);
        profile.Occupation = ddlOccupations.SelectedValue;
        profile.Website = txtWebsite.Text;
        profile.Address.Street = txtStreet.Text;
        profile.Address.City = txtCity.Text;
        profile.Address.PostalCode = txtPostalCode.Text;
        profile.Address.State = txtState.Text;
        profile.Contacts.Phone = txtPhone.Text;
        profile.Contacts.Fax = txtFax.Text;
        profile.Save();
    }

}

Upvotes: 2

Views: 5895

Answers (3)

Steve Hibbert
Steve Hibbert

Reputation: 2165

There is a very simple work-around for this, for all coders who just want to hack on with things. You can get the ProfileBase type and load the profile into that, but you lose strong typing. If you are in control of the data in the profile, or you are sure that the data in the profile is of a certain type, you are good to go.

    string user = "Steve"; // The username you are trying to get the profile for.
    bool isAuthenticated = false;

        MembershipUser mu = Membership.GetUser(user);

        if (mu != null)
        {
            // User exists - Try to load profile 

            ProfileBase pb = ProfileBase.Create(user, isAuthenticated);

            if (pb != null)
            {
                // Profile loaded - Try to access profile data element.
                // ProfileBase stores data as objects in (I assume) a Dictionary 
                // so you have to cast and check that the cast succeeds.

                string myData = (string)pb["MyKey"];

                if (!string.IsNullOrWhiteSpace(myData))         
                {
                    // Woo-hoo - We're in data city, baby!
                    Console.WriteLine("Is this your card? " + myData + " - Ta Dah!");
                }
            }       
       }

Upvotes: 0

IrishChieftain
IrishChieftain

Reputation: 15253

As Mark pointed out, profiles only work out-of-the-box with the website template and I have blogged instructions on how to use the plug-in to facilitate the use of profiles for the Web Application project:

http://www.codersbarn.com/post/2008/07/10/ASPNET-PayPal-Subscriptions-IPN.aspx

It is possible to do it yourself, and here's a fully working implementation that you can download:

http://leedumond.com/blog/asp-net-profiles-in-web-application-projects/

Upvotes: 2

Mark Hall
Mark Hall

Reputation: 54532

According to these links(link1, link2)

Web Applications don't support the auto generation of the ProfileCommon object

The first link then give's a link to a VS Addin and instructions on how to incorporate it into the build process in order to work around the problem

Upvotes: 1

Related Questions