Profile. What am I doing wrong?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Profile. What am I doing wrong?

    Hello,

    I am using a custom Profile provider as follows:

    public class Profile : ProfileBase {

    [SettingsAllowAn onymous(false)]
    public Bio Bio { get; set; }

    [SettingsAllowAn onymous(true)]
    public Visitor Visitor { get; set; }

    public static Profile Get() {
    return Create(Membersh ip.GetUser().Us erName) as Profile;
    }
    public static Profile Get(string username) {
    return Create(username ) as Profile;
    }

    }

    Visitor is a class has follows:

    [Serializable()]
    public class Visitor {
    public CultureInfo Culture { get; set; }
    }

    On my web page I am overriding InitializeCultu re, by getting the value
    of Visitor.Culture :

    1 protected override void InitializeCultu re() {
    2 base.Initialize Culture();
    3 CultureInfo culture = Profile.Get().V isitor.Culture;
    4 if (culture != null) {
    5 Thread.CurrentT hread.CurrentCu lture = culture;
    6 Thread.CurrentT hread.CurrentUI Culture = culture;
    7 }
    8 }

    Because of code line 3 I always get an error on my profile provider:

    1 public static Profile Get() {
    2 return Create(Membersh ip.GetUser().Us erName) as Profile;
    3 }

    The error is on line 2 and says:

    "Object reference not set to an instance of an object. Use the new
    keyword to create an object instance."

    What am I doing wrong?

    Thanks,

    Miguel
  • shapper

    #2
    Re: Profile. What am I doing wrong?

    On Oct 9, 10:32 pm, shapper <mdmo...@gmail. comwrote:
    Hello,
    >
    I am using a custom Profile provider as follows:
    >
      public class Profile : ProfileBase {
    >
        [SettingsAllowAn onymous(false)]
        public Bio Bio { get; set; }
    >
        [SettingsAllowAn onymous(true)]
        public Visitor Visitor { get; set; }
    >
        public static Profile Get() {
          return Create(Membersh ip.GetUser().Us erName) as Profile;
        }
        public static Profile Get(string username) {
          return Create(username ) as Profile;
        }
    >
      }
    >
    Visitor is a class has follows:
    >
      [Serializable()]
      public class Visitor {
        public CultureInfo Culture { get; set; }
      }
    >
    On my web page I am overriding InitializeCultu re, by getting the value
    of Visitor.Culture :
    >
    1        protected override void InitializeCultu re() {
    2          base.Initialize Culture();
    3          CultureInfo culture = Profile.Get().V isitor.Culture;
    4          if (culture != null) {
    5            Thread.CurrentT hread.CurrentCu lture = culture;
    6            Thread.CurrentT hread.CurrentUI Culture = culture;
    7          }
    8        }
    >
    Because of code line 3 I always get an error on my profile provider:
    >
    1        public static Profile Get() {
    2          return Create(Membersh ip.GetUser().Us erName) as Profile;
    3        }
    >
    The error is on line 2 and says:
    >
    "Object reference not set to an instance of an object. Use the new
    keyword to create an object instance."
    >
    What am I doing wrong?
    >
    Thanks,
    >
    Miguel
    I changed my code to the following and debbuged it:

    Profile profile = Profile.Get(Use r.Identity.Name );
    CultureInfo culture = profile.Visitor .Culture;

    Now profile is defined but Visitor is null ... So now I have the same
    error, "Object reference not set to an instance of an object." on code
    line 2.

    The user is an anonymous user and it is the first time that accesses
    the web site so it is normal that none of the profile properties have
    been defined.

    How should I prevent this error?

    Thanks,

    Miguel

    Comment

    Working...