[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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With