Lost
Lost

Reputation: 13575

Unable to cast object of type 'System.Web.Profile.DefaultProfile' to type 'ProfileCommon'

I am seeing this error:

Unable to cast object of type 'System.Web.Profile.DefaultProfile' to type 'ProfileCommon'.

on following code:

ProfileCommon p = (ProfileCommon)ProfileCommon.Create(TextUsername.Text, true);

I am not sure why...

Upvotes: 4

Views: 7213

Answers (2)

santosh singh
santosh singh

Reputation: 28652

Agree with theChrisKent.It's look like problem in the web.config

Check out the following example for,How to get ProfileCommon object in asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="GetProfile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        User Name:<asp:TextBox ID="txtUserName" runat="server"/>
        <asp:Button ID="cmdGet" runat="server" OnClick="cmdGet_Click" Text="Get Profile" /><br />
        <asp:Label ID="lbl" runat="server" Text=""/>
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GetProfile : System.Web.UI.Page
{
  protected void cmdGet_Click(object sender, EventArgs e)
  {
    ProfileCommon profile = Profile.GetProfile(txtUserName.Text);
        if (profile.LastUpdatedDate == DateTime.MinValue)
        {
            lbl.Text = "No user match found.";
        }
        else
        {
            lbl.Text = "This user lives in " + profile.FirstName;
        }
  }
}

File: Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <profile>
      <properties>
        <add name="FirstName" type="String" serializeAs="Binary"/>
        <add name="LastName" type="String" serializeAs="Xml"/>
        <add name="DateOfBirth" type="DateTime" serializeAs="String"/>
      </properties>
    </profile>
  </system.web>
</configuration>

EDIT

This example only work when the project type is website.If you want to use ProfileCommon object in Web Application then go through the following link

Converting Profile Object Code

EDIT -II

As per your comment,Following link might help you

ASP.NET Profiles in Web Application Projects

Upvotes: 3

theChrisKent
theChrisKent

Reputation: 15099

Check your web.config if no <profile /> section is defined, ASP.NET creates a profile object of type DefaultProfile which can cause your error.

The above only applies to Web Sites and not Web Applications. Since you have indicated you are using a Web Application you should be aware:

  • Visual Studio does NOT generate ProfileCommon objects for Web Applications
  • You have to access your profile properties using ProfileBase.GetPropertyValue(PropertyName)

So you can get a property like this (example from below article):

DateTime DOB = Convert.ToDateTime(HttpContext.Current.Profile.GetPropertyValue("DateOfBirth"));

See this article for more information/examples: http://weblogs.asp.net/anasghanem/archive/2008/04/12/the-differences-in-profile-between-web-application-projects-wap-and-website.aspx

Upvotes: 1

Related Questions