Reputation: 5104
I am using EWS to develop my email client. I found that if I store ItemId in viewstate it will cause an exception says:
Type 'Microsoft.Exchange.WebServices.Data.ItemId' in Assembly 'Microsoft.Exchange.WebServices, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
If I store ItemId
as string like:
ViewState["itemId"] = id.ToString();
and then try to cast back,
ItemId id = (ItemId)ViewState["itemId"];
it says I can not convert from string to ItemId
. Any idea?
Upvotes: 5
Views: 28280
Reputation: 6999
You are storing string and expecting ItemId. You should store as
ItemId itemId = new ItemId();
ViewState["itemId"] = itemId;
However, since ItemId is not seriaziable, it cannot be stored. To store it make your serializable class inherited from ItemId and override all the members and store it in ViewState
[Serializable]
public class MyItemId: ItemId {
// override all properties
}
Store like this
MyItemId itemId = new MyItemId();
ViewState["itemId"] = itemId;
and retrieve
MyItemId id=(MyItemId)ViewState["itemId"];
Upvotes: 8
Reputation: 2737
You cannot store it in ViewState.
However you could probably store it in Session, since it uses the binary formatter.
ViewState is serialized with the LosFormatter class.
Upvotes: 0
Reputation: 13442
As the error message suggests, you can't store an object in viewstate unless it's marked as serializable.
Looking at the documentation here, it seems that the ItemId class has a UniqueId property, which is a string, and a constructor which takes a string 'uniqueId' parameter.
So, can you store the uniqueId in viewstate, and regenerate the object using the constructor?
Upvotes: 3
Reputation: 48240
Did you try:
ItemId id = new ItemId( ViewState["itemId"] );
Upvotes: 6