Difficulties with SqlProfileProvider - where to get the SettingsContext object from?

Last post 06-10-2008 3:37 AM by jahabdank. 2 replies.

Sort Posts:

  • Difficulties with SqlProfileProvider - where to get the SettingsContext object from?

    06-09-2008, 10:29 AM
    • Member
      53 point Member
    • jahabdank
    • Member since 07-06-2007, 5:11 PM
    • Posts 143

    Hello,

    I am tryng to use the built it Providers (Membership, Profile etc.). I have problems with SqlProfileProvider. I try to run exampe code from: http://msdn.microsoft.com/en-us/library/system.web.profile.sqlprofileprovider.setpropertyvalues(VS.80).aspx

    1    SqlProfileProvider p = 
    2      (SqlProfileProvider)Profile.Providers["SqlProvider"];
    3    
    4    SettingsPropertyValueCollection pvalues = 
    5      p.GetPropertyValues(Profile.Context, ProfileBase.Properties);
    6    
    7    pvalues["ZipCode"].PropertyValue = "98052";
    8    pvalues["CityAndState"].PropertyValue = "Redmond, WA";
    9    
    10   p.SetPropertyValues(Profile.Context, pvalues);
    

    but it neither explains where does the SettingsContext comes from, nor what 'Profile' object is .... (what type and where to get it from. It is surely not the ProfileManager, because it does not have a Context field).

    Thanks in advance,

    Jozef A. Habdank

  • Re: Difficulties with SqlProfileProvider - where to get the SettingsContext object from?

    06-10-2008, 3:05 AM
    Answer

    jahabdank:

    but it neither explains where does the SettingsContext comes from, nor what 'Profile' object is .... (what type and where to get it from. It is surely not the ProfileManager, because it does not have a Context field).

    Hi

    The SettingsContext object is derived from Hashtable and contains two key-value pairs:["UserName" and "IsAuthenticated"].

    "UserName" mean the name of the user whose profile you want to retrieve. By default, it's current authenticated user. and generate from page context using: HttpContext.Current.User.Identity.Name, However, you can also load any user's profile data into an HttpProfile objectl
    If anonymous identification is enabled, the "UserName" value returned is a Guid to identify the anonymous user.

    Best Regards
    XiaoYong Dai
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Difficulties with SqlProfileProvider - where to get the SettingsContext object from?

    06-10-2008, 3:37 AM
    • Member
      53 point Member
    • jahabdank
    • Member since 07-06-2007, 5:11 PM
    • Posts 143

    Thanks a lot.

    So just for the sake of people who will come here later, the code in the code behind (meaning from the within the Page object) can look like that (I really wonder why sombody did not write it this way in the MSDN class description):

    1                    // Providers is a static method in the ProfileManager class
    2                    System.Web.Profile.SqlProfileProvider p = (System.Web.Profile.SqlProfileProvider)System.Web.Profile.ProfileManager.Providers["SqlProvider"];
    3    
    4                    // ProfileBase.Properties is a static collection of properties defined in the web config:
    5                    //<profile defaultProvider="SqlProvider">
    6                    //  <providers>
    7                    //    <clear />
    8                    //    <add name="SqlProvider"
    9                    //      type="System.Web.Profile.SqlProfileProvider"
    10                   //      connectionStringName="ASPNETDBConnectionString"
    11                   //      applicationName="SampleApplication"
    12                   //      description="SqlProfileProvider for SampleApplication" />
    13                   //  </providers>
    14                   //  <properties> --cmt:2abe902b-fbad-4ab1-905c-c40df7587bc0--
    15                   //    <add name="ZipCode" />
    16                   //    <add name="CityAndState" />
    17                   //  </properties>
    18                   //</profile>
    19                   SettingsPropertyValueCollection pvalues =
    20                     p.GetPropertyValues(this.Context.Profile.Context, System.Web.Profile.ProfileBase.Properties);
    21   
    22                   // you can get or set the values
    23                   pvalues["ZipCode"].PropertyValue = "98052";
    24                   pvalues["CityAndState"].PropertyValue = "Redmond, WA";
    25   
    26                   // in order to update, pass the current Profile Context, and the values
    27                   p.SetPropertyValues(this.Context.Profile.Context, pvalues);
    
     
     
Page 1 of 1 (3 items)