Serialize problem with cookie
        Posted  
        
            by cagin
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by cagin
        
        
        
        Published on 2010-06-02T10:29:57Z
        Indexed on 
            2010/06/02
            10:33 UTC
        
        
        Read the original article
        Hit count: 307
        
Hi there,
I want use cookie in my web project. I must serialize my classes. Although my code can seralize an int or string value, it cant seralize my classes. This is my seralize and cookie code :
    public static bool f_SetCookie(string _sCookieName, object _oCookieValue, DateTime _dtimeExpirationDate)
    {
        bool retval = true;
        try
        {
            if (HttpContext.Current.Request[_sCookieName] != null)
            {
                HttpContext.Current.Request.Cookies.Remove(_sCookieName);
            }
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, _oCookieValue);
            byte[] bArr = ms.ToArray();
            MemoryStream objStream = new MemoryStream();
            DeflateStream objZS = new DeflateStream(objStream, CompressionMode.Compress);
            objZS.Write(bArr, 0, bArr.Length);
            objZS.Flush();
            objZS.Close();
            byte[] bytes = objStream.ToArray();
            string sCookieVal = Convert.ToBase64String(bytes);
            HttpCookie cook = new HttpCookie(_sCookieName);
            cook.Value = sCookieVal;
            cook.Expires = _dtimeExpirationDate;
            HttpContext.Current.Response.Cookies.Add(cook);
        }
        catch
        {
            retval = false;
        }
        return retval;
    }
And here is one of my classes:
[Serializable]
    public class Tahlil
    {
        #region Props & Fields
        public string M_KlinikKodu{ get; set; }
        public DateTime M_AlinmaTarihi { get; set; }
        private List<Test> m_Tesler;
        public List<Test> M_Tesler
        {
            get { return m_Tesler; }
            set { m_Tesler = value; }
        }
        #endregion
       public Tahlil() {}
       Tahlil(DataRow _rwTahlil){}           
    }
I m calling my Set Cookie method:
        Tahlil t = new Tahlil();
        t.M_AlinmaTarihi = DateTime.Now;
        t.M_KlinikKodu = "2";
        t.M_Tesler = new List<Test>();
        f_SetCookie("Tahlil", t, DateTime.Now.AddDays(1));
I cant see cookie in Cookie folder and Temporary Internet Files but if i will call method like that:
       f_SetCookie("TRY", 5, DateTime.Now.AddDays(1));
I can see cookie. What is the problem? I dont understand.
Thank you for your helps.
© Stack Overflow or respective owner