Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we save KeyValuePair<K,V> in user profile?

[Serializable]
public class KeyValue : ProfileBase
{
    public KeyValue() { }

    public KeyValuePair<string, string> KV
    {
        get { return (KeyValuePair<string, string>)base["KV"]; }
        set { base["KV"] = value; }
    }            
}

public void SaveProfileData()
{
    KeyValue profile = (KeyValue) HttpContext.Current.Profile;
    profile.Name.Add(File);
    profile.KV = new KeyValuePair<string, string>("key", "val"); 
    profile.Save();
}   

public void LoadProfile()
{
    KeyValue profile = (KeyValue) HttpContext.Current.Profile;
    string k = profile.KV.Key;
    string v = profile.KV.Value;
    Files = profile.Name;          
}

I am trying to save KeyValuePair<K,V> in asp.net userprofile and it saves also but when i am accessing it, it show both key and value property null, can anybody tells me where i am wrong?

In LoadProfile() k and v are null.

Web.config

<profile enabled="true" inherits="SiteBuilder.Models.KeyValue">
  <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
</profile>
like image 959
Gaurav Avatar asked May 10 '12 12:05

Gaurav


1 Answers

C#'s KeyValuePair has not a public setter for the Key / Value attributes. So it might serialize but it will deserialize empty.

You must create your own little implementation of the class, for example:

[Serializable]
[DataContract]
public class KeyValue<K,V>
{
    /// <summary>
    /// The Key
    /// </summary>
    [DataMember]
    public K Key { get; set; }

    /// <summary>
    /// The Value
    /// </summary>
    [DataMember]
    public V Value { get; set; }
}

And then use it in your example.

like image 65
Adrian Salazar Avatar answered Oct 19 '22 23:10

Adrian Salazar