Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
protected override void LoadToView(CaseStageDomObj caseStage)
        {
                Button btn = (Button)((ViewComplaint)Page).ReturnTheButton();
                Button _button = (Button)Serialize<Button>(btn);
                ViewState.Add("Button", _button);
        }

public static MemoryStream Serialize<Button>(Button btn)
        {
            
            DataContractSerializer s = new DataContractSerializer(typeof(Button));
            MemoryStream ms = new MemoryStream();
            s.WriteObject(ms, btn);
            return ms;
        } 


Why its showing the following error

Cannot convert type 'System.IO.MemoryStream' to 'System.Web.UI.WebControls.Button'
Posted
Updated 25-Nov-11 9:40am
v2
Comments
RaviRanjanKr 25-Nov-11 15:42pm    
A suggestion :- Always wrap your code in "Pre" tag.
rajh7 25-Nov-11 15:44pm    
what does it mean? could you show me please
Sergey Alexandrovich Kryukov 25-Nov-11 15:52pm    
Please use "Improve question" and look at the formatting of the code as Ravi did for you. Do you know HTML, anyway?
--SA
Sergey Alexandrovich Kryukov 25-Nov-11 16:01pm    
Please check up: it looks like the posted code and the code showing this error are different.
Indicate the on which where the exception is reported.
--SA

1 solution

The return of your call to: Serialize<button>(btn): is a System.IO.MemoryStream object that holds the serialized form of the Button.

You can't just "cast" that back to the Button object itself.

You have to de-serialize the MemoryStream. You can use the same DataContractSerializer you use to serialize, but, in MS' words: "To retrieve data, create an object appropriate to reading the data format (such as an XmlDictionaryReader for an XML document) and call the ReadObject method."

best, Bill
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900